Tag: C#

  • How To Use the Box API To Adjust User Properties and Make Read-Only in Bulk (w/example)

    • Get up to date on the APIs you need for users @ https://developer.box.com/v2.0/reference
    • Sign up for your developer account at your enterprise @ https://yourenterprise.app.box.com/developers/console (replace with your enterprise application name in the link above)
    • Create your Box app & API Key – link here and here
    • Pull down the Box Windows SDK @ – https://github.com/box/box-windows-sdk-v2
    • Update the pem file with your actual pem information from the app you created
      • in case you’re wondering it should contain the —–BEGIN ENCRYPTED PRIVATE KEY—– and —–END ENCRYPTED PRIVATE KEY—–
    • Update the properties appropriately in the app.config

    [sourcecode]
    <appSettings>
    <add key="boxClientId" value="uniquestringhere" />
    <add key="boxClientSecret" value="uniquestringheretoo" />
    <add key="boxEnterpriseId" value="1234567" />
    <add key="boxPrivateKeyPassword" value="uniquestringhereforpassword" />
    <add key="boxPublicKeyId" value="uniquepublickeyid" />
    <add key="ClientSettingsProvider.ServiceUri" value="useifneeded" />
    </appSettings>
    [/sourcecode]

    • From there you can update your Main runner in the program.cs appropriately. In the case of my sample below I am getting authorized, retrieving my users and looping through everyone but the main admin account and making them read-only. I’m also writing the information out to a file so I’ll have the logs of who was set / not set.

    [sourcecode]

    var privateKey = File.ReadAllText("private_key.pem");

    var boxConfig = new BoxConfig(CLIENT_ID, CLIENT_SECRET, ENTERPRISE_ID, privateKey, JWT_PRIVATE_KEY_PASSWORD, JWT_PUBLIC_KEY_ID);
    var boxJWT = new BoxJWTAuth(boxConfig);

    var adminToken = boxJWT.AdminToken();
    Console.WriteLine("Admin Token: " + adminToken);
    Console.WriteLine();

    var adminClient = boxJWT.AdminClient(adminToken);

    var items = await adminClient.UsersManager.GetEnterpriseUsersAsync("", 0, 1000);
    items.Entries.ForEach(async i =>
    {
    if (i.Login != "myspecialadminaccount@domain.com")
    {
    BoxUserRequest userRequest = new BoxUserRequest()
    {
    Id = i.Id,
    Status = "cannot_delete_edit_upload"
    };
    System.Console.WriteLine("\t{0}", i.Name);
    System.Console.WriteLine("\t{0}", i.Id);
    System.Console.WriteLine("\t{0}", i.Login);
    System.Console.WriteLine("\t{0}", i.Type);
    System.Console.WriteLine(" ");
    // Turn on for prod
    BoxUser user = await adminClient.UsersManager.UpdateUserInformationAsync(userRequest);
    Console.WriteLine(userRequest.Name + "updated to read-only");
    System.Threading.Thread.Sleep(2000);
    stringtext = i.Name.ToString() + " – " + i.Id.ToString() + " – " + i.Login.ToString() + " – " + i.Type.ToString() + " – updated to readonly" + Environment.NewLine;
    System.Console.WriteLine(" ");

    }
    else
    {
    BoxUserRequest userRequest = new BoxUserRequest()
    {
    Id = i.Id
    };
    System.Console.WriteLine("\t{0}", i.Name);
    System.Console.WriteLine("\t{0}", i.Id);
    System.Console.WriteLine("\t{0}", i.Login);
    System.Console.WriteLine("\t{0}", i.Type);
    System.Console.WriteLine(" ");
    Console.WriteLine(userRequest.Name + " NOT updated to read-only");
    System.Threading.Thread.Sleep(2000);
    stringtext = i.Name.ToString() + " – " + i.Id.ToString() + " – " + i.Login.ToString() + " – " + i.Type.ToString() + " – NOT updated to readonly" + Environment.NewLine;
    System.Console.WriteLine(" ");

    }
    });
    File.AppendAllText("C:\\Temp\\" + "log.txt", stringtext);
    [/sourcecode]

    • Enable your box application to your enterprise accounts – link here
    • Run your app (In TEST with test accounts! Then, with approval, prod.)
      • If the app won’t run…check:
        • code syntax errors
        • certificate pem
        • values in the app config
        • authorization status in the admin Console on Box
        • that your app is enabled and still available on the enterprise developer site

    Hope this helps! I can send the source out on request if needed (just let me know).

  • Stuck On “Cannot Load php7apache2_4.dll into server: The specified module could not be found”? Try this…

    I recently just performed a PHP upgrade from 5.6.15 to 7.0.1. After doing everything I had suspected I needed to do I received this error in my logs when trying to start Apache:

    httpd.exe: Syntax error on line 569 of C:/Program Files (x86)/Apache Software Foundation/Apache24/conf/httpd.conf: Cannot load C:/Program Files (x86)/PHP.7.0.1/php7apache2_4.dll into server: The specified module could not be found.

    Here is what I had in that block, bolding the item in question:

    LoadModule php7_module "C:/Program Files (x86)/PHP.7.0.1/php7apache2_4.dll"
    AddHandler application/x-httpd-php .php
    PHPIniDir "C:/Program Files (x86)/PHP.7.0.1/"

    Huh??? I checked and double checked and that file was there. After about 2 hours of scratching my head I then found a forum post that clicked for me. You may not be recognizing the .dll file because it has a dependency on the Visual C++ Redistributable for Visual Studio 2015. Once I downloaded and installed the exe files (located here) then after another restart I started seeing my pages again. Hope it helps.

  • Use No Captcha reCaptcha In ASP.Net Web Application Form Page With C#, JSON and JavaScript

    Users are getting more and more tired of interpreting images with random numbers or letters that take 2-3 tries to get. Google has a new release of the reCaptcha to include a  “No Captcha” feature. You can read about that more here. Here are the steps that I took to include this newer version into a ASP.Net web application.

    • Obtain a google account and sign up for reCaptcha 
    • Create a Visual Studio C# based web forms application and create a page where you will have your reCaptcha to live
    • Download Json via NuGet (instructions are here)
    • Once you have reCaptcha information be sure to insert the following in the head section of your web page:

    [sourcecode language=”javascript”]
    <script src="https://www.google.com/recaptcha/api.js&quot; type="text/javascript"></script>
    [/sourcecode]

    • Then place in your web page where you want the reCaptcha to live. Remember to replace “yoursitekey” with the Google reCaptcha site key:

    [sourcecode]
    <div class="g-recaptcha" data-sitekey="yoursitekey"></div>
    [/sourcecode]

     

    • Create a class in your web application called ReCaptchaClass (credit for class and credit for proxy) and put the following code into the class. Remember to replace “yoursecretkeygoeshere” with the Google reCaptcha secret key:

    [sourcecode]
    using Newtonsoft.Json;
    public class ReCaptchaClass
    {
    public static string Validate(string EncodedResponse)
    {
    var client = new System.Net.WebClient();
    IWebProxy defaultWebProxy = WebRequest.DefaultWebProxy;
    defaultWebProxy.Credentials = CredentialCache.DefaultCredentials;
    client.Proxy = defaultWebProxy;
    string PrivateKey = "yoursecretkeygoeshere";
    var GoogleReply = client.DownloadString(string.Format("https://www.google.com/recaptcha/api/siteverify?secret={0}&amp;response={1}", PrivateKey, EncodedResponse));
    var captchaResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(GoogleReply);
    return captchaResponse.Success;
    }
    [JsonProperty("success")]
    public string Success
    {
    get { return m_Success; }
    set { m_Success = value; }
    }

    private string m_Success;
    [JsonProperty("error-codes")]
    public List ErrorCodes
    {
    get { return m_ErrorCodes; }
    set { m_ErrorCodes = value; }
    }
    private List m_ErrorCodes;
    }
    [/sourcecode]

    • Once the class is created place the following snippet in the code behind of your page (likely in a button click event of some kind):

    [sourcecode]
    string EncodedResponse = Request.Form["g-Recaptcha-Response"];
    bool IsCaptchaValid = (ReCaptchaClass.Validate(EncodedResponse) == "True" ? true : false);

    if (IsCaptchaValid) {
    //Valid Request
    }
    [/sourcecode]
    That’s it! Easy enough, right? I hope it helps. Questions are always welcome.

  • Write a JavaScript/HTML Form Submit Post From C# APSX Code Behind

    There may be a case where you are taking information from a form and need to post it to a web site somewhere else. One quick method of performing that post is to execute it from the code behind using C# (via context.response) and JavaScript. This is a scaled down example of a case where I was submitting a POST from the code behind of a web part control:

    A couple things to note:

    • You can add as many fields as you wish to post, I am only using two
    • You can use string.Format to get C# variables to put directly in the POST

    [sourcecode]
    protected string value1;
    protected string value2;

    value1 = "something";
    value2 = "something else";

    Context.Response.Write("&lt;form name=’Form1′ id=’Form1′ method=’post’ action=’https://www.mywebsitetopostto.com/example’&gt;&quot;);
    Context.Response.Write("&lt;input type=’hidden’ name=’__VIEWSTATE’ id=’__VIEWSTATE’ value=” /&gt;");
    Context.Response.Write(string.Format("&lt;input type=’hidden’ name=’txtValue1′ id=’txtValue1′ runat=’server’ value='{0}’ /&gt;", value1));
    Context.Response.Write(string.Format("&lt;input type=’hidden’ name=’txtValue2′ id=’txtValue2′ runat=’server’ value='{0}’ /&gt;", value2));
    Context.Response.Write("&lt;/form&gt;");
    Context.Response.Write("document.Form1.submit();");
    Context.Response.Flush();
    Context.Response.End();
    [/sourcecode]
    Ideally you would use this in a case where you are posting content and the page you are posting to will process and serve something after the form that receives your post processes what you sent. Please do not use this snippet and blame me for a blank page 🙂

  • Convert A Links List To A DropDownList By A Web Part In SharePoint 2013 (C#)

    So I have been enjoying the benefit of Mike Smith’s article for a while now (http://techtrainingnotes.blogspot.com/2010/12/sharepoint-convert-links-list-to.html). A client I did a project for was wanting to carry this functionality forward to 2013. The problem: this method didn’t work in 2013.

    So I took a programmatic approach to lose the JavaScript dependency. It also will sort the dropdown names by what you type as the description by using the sortedlist in the code behind. It’s fairly simple, here’s what I did:

    • Created a links list and populated some URLs and Descriptions (!!! You need both for below to work, otherwise remove the description dependency in my code !!!)
    • Create a SharePoint 2013 Visual Web Part project
    • Add the dropdownlist control to the control

    In the .ascx control I have:
    [sourcecode]
    <asp:DropDownList ID="linkDropDown" runat="server" Font-Names="Arial" Font-Size="10pt"
    Width="300px" OnSelectedIndexChanged="linkDropDown_SelectedIndexChanged" AutoPostBack="true" EnableViewState="true">
    <asp:ListItem Text="Link to …" Value="#"></asp:ListItem>
    </asp:DropDownList>
    [/sourcecode]

    • Add the code to the code behind of the control

    Now the code behind, here’s my using statements:
    [sourcecode]
    using System;
    using System.ComponentModel;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using System.Collections;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    [/sourcecode]
    and here is what I have in the Page_Load and the SelectedIndexChanged event
    [sourcecode]
    protected void linkDropDown_SelectedIndexChanged(object sender, EventArgs e)
    {
    Context.Response.Redirect(linkDropDown.SelectedValue.ToString());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    SortedList sortedLists = new SortedList();

    using (SPSite siteCol = new SPSite("http://mysharepoint2013site/&quot;))
    {
    using (SPWeb web = siteCol.RootWeb)
    {
    SPList list = web.GetList("/Lists/NameOfLinksList");
    SPListItemCollection items = list.GetItems();

    foreach (SPListItem it in items)
    {
    SPFieldUrlValue fieldValue = new SPFieldUrlValue(it["URL"].ToString());
    string description = fieldValue.Description;
    string url = fieldValue.Url;
    sortedLists.Add(description, url);
    }
    }

    IDictionaryEnumerator enumerator = sortedLists.GetEnumerator();
    while (enumerator.MoveNext())
    {
    ListItem listitem = new ListItem();
    listitem.Text = enumerator.Key.ToString();
    listitem.Value = enumerator.Value.ToString();
    linkDropDown.Items.Add(new ListItem(listitem.Text.ToString(), listitem.Value.ToString()));
    }
    }
    [/sourcecode]
    Deploy and enjoy!

  • Sluggish Sitecore Development Environment? Here’s An Easy Method To Disable Sitecore Analytics

    When you install a new 8.0 instance of Sitecore you will find the site can be laggy and sluggish to load. To speed up your dev environment I would recommend this easy fix.

    First create a folder called z in your app config sitecore path: C:\inetpub\wwwroot\yoursitecore\Website\App_Config\z

    Then, create a file called zwoa.config and paste the following in:
    [sourcecode]
    <configuration xmlns:patch="<a href="http://www.sitecore.net/xmlconfig/">http://www.sitecore.net/xmlconfig/</a>"&gt;
    <sitecore>
    <pipelines>
    <initialize>
    <processor type="Sitecore.Pipelines.Initialize.PrecompileSpeakViews, Sitecore.Speak.Clientpro" >
    <patch:delete />
    </processor>
    <processor type="Sitecore.ContentTesting.Pipelines.Initialize.RegisterContentTestingCommandRoute, Sitecore.ContentTesting" >
    <patch:delete />
    </processor>
    <processor type="Sitecore.Pipelines.Initialize.PrecompileSpeakViews, Sitecore.Speak.Client" use="ContentTesting" >
    <patch:delete />
    </processor>
    </initialize>
    <mvc.renderPageExtenders>
    <processor patch:after="processor[@type=’Sitecore.Mvc.ExperienceEditor.Pipelines.RenderPageExtenders.RenderExtendersContainer, Sitecore.Mvc.ExperienceEditor’]" type="Sitecore.Mvc.ExperienceEditor.Pipelines.RenderPageExtenders.RenderPageEditorExtender, Sitecore.Mvc.ExperienceEditor"></processor>
    <processor patch:after="processor[@type=’Sitecore.Mvc.ExperienceEditor.Pipelines.RenderPageExtenders.RenderExtendersContainer, Sitecore.Mvc.ExperienceEditor’]" type="Sitecore.Mvc.ExperienceEditor.Pipelines.RenderPageExtenders.RenderPreviewExtender, Sitecore.Mvc.ExperienceEditor"></processor>
    <processor patch:after="processor[@type=’Sitecore.Mvc.ExperienceEditor.Pipelines.RenderPageExtenders.RenderExtendersContainer, Sitecore.Mvc.ExperienceEditor’]" type="Sitecore.Mvc.ExperienceEditor.Pipelines.RenderPageExtenders.RenderDebugExtender, Sitecore.Mvc.ExperienceEditor"></processor>
    <processor type=""Sitecore.Mvc.ExperienceEditor.Pipelines.RenderPageExtenders.SpeakRibbon.RenderPageEditorSpeakExtender, Sitecore.Mvc.ExperienceEditor" >
    <patch:delete />
    </processor>
    </mvc.renderPageExtenders>
    </pipelines>
    <settings>
    <setting name="Analytics.Enabled" > <patch:attribute name="value" value="false" />
    </setting>
    </settings>
    </sitecore>
    </configuration>
    [/sourcecode]
    Refresh your Sitecore in a web browser and watch it go!

  • C# Send A SMS Text Message Programmatically Using Clickatell HTTP API

    I would start off by heading here to better understand the Clickatell HTTP API: https://www.clickatell.com/apis-scripts/apis/http-s/. If you are interested they allow you 10 free text messages to try it out before you buy a package. Just sign up for an account and create your HTTP API and use your information to populate the snippet below.

    For a particular project I am using this API to send out texts for verification purposes. I am using my method inside of a web service to send the information about a user as well as their pre-generated pin. Of course it is a lot different in my project but I am providing the bare bones below.

    If you are a skimmer and you skipped reading anything I said above then here you go:

    Code for MyWebRequest class:
    [sourcecode]
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Net;
    using System.IO;
    using System.Text;
    using System.Configuration;

    namespace CustomWebServices
    {
    public class MyWebRequest
    {
    private WebRequest request;
    private Stream dataStream;

    private string status;

    public String Status
    {
    get
    {
    return status;
    }
    set
    {
    status = value;
    }
    }

    public MyWebRequest(string url)
    {
    // Create a request using a URL that can receive a post.

    request = WebRequest.Create(url);
    }

    public MyWebRequest(string url, string method)
    : this(url)
    {

    if (method.Equals("GET") || method.Equals("POST"))
    {
    // Set the Method property of the request to POST.
    request.Method = method;
    }
    else
    {
    throw new Exception("Invalid Method Type");
    }
    }

    public MyWebRequest(string url, string method, string data)
    : this(url, method)
    {

    // Create POST data and convert it to a byte array.
    string postData = data;
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);

    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";

    // Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length;

    // Get the request stream.
    dataStream = request.GetRequestStream();

    // Write the data to the request stream.
    dataStream.Write(byteArray, 0, byteArray.Length);

    // Close the Stream object.
    dataStream.Close();

    }

    public string GetResponse()
    {
    // Get the original response.
    WebResponse response = request.GetResponse();

    this.Status = ((HttpWebResponse)response).StatusDescription;

    // Get the stream containing all content returned by the requested server.
    dataStream = response.GetResponseStream();

    // Open the stream using a StreamReader for easy access.
    StreamReader reader = new StreamReader(dataStream);

    // Read the content fully up to the end.
    string responseFromServer = reader.ReadToEnd();

    // Clean up the streams.
    reader.Close();
    dataStream.Close();
    response.Close();

    return responseFromServer;
    }
    }
    }
    [/sourcecode]
    And now the code for the method itself:
    [sourcecode]
    public string SendText(string mobile, string pin)
    {
    // Make sure to add the country code to ensure the text goes through
    string phoneCheck = "";
    if (mobile.Length == 10)
    {
    phoneCheck = "1" + mobile;
    }
    // Clickatell settings
    string textmessage = "Your verification pin is " + pin;
    string parameters = "";
    parameters = "user=yourclickatellusername";
    parameters = parameters + "&amp;password=yourclickatellpassword";
    parameters = parameters + "&amp;api_id=youruniqueclickatellhttpapiid";
    parameters = parameters + "&amp;to=" + phoneCheck;
    parameters = parameters + "&amp;text=" + textmessage;
    parameters = parameters + "&amp;mo=1";
    parameters = parameters + "&amp;from=15558675309";
    MyWebRequest myRequest = new MyWebRequest("<a href="http://api.clickatell.com/http/sendmsg">http://api.clickatell.com/http/sendmsg</a>&quot;, "POST", parameters);
    myRequest.GetResponse();
    } // SendText
    [/sourcecode]
    * Note: If you put this call on a server that will require a proxy then add it to your MyWebRequest class

  • Add An Item To A Custom SharePoint List Using Batch XML With A Web Service (C#)

    If you have multiple active directory environments and SharePoint instances in both of those environments then you will understand why I posted this.

    I received a requirement that while users from a separate domain and SharePoint instance would need to fill out a form on their SharePoint and have the form submission be sent to the other SharePoint instance (on the other domain). This was because they did not want to mix and mingle with forest trusting or having users to have two separate AD accounts to manage the information. Say what?!

    After some initial discovery I had reached a couple quick conclusions:

    – SharePoints on different domains do not talk well to each other without trust

    – Using the built-in lists service on SharePoint has to be done on the server itself unless you are going to have an instance stood up somewhere on the same domain that has all the required SharePoint dlls to be referenced

    All being said, here is an example of what I used as a web service I placed on the SharePoint server that calls the lists web service doing a list add using batch XML:
    [sourcecode]
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint.Administration;
    using Microsoft.SharePoint.Administration.Claims;
    using Microsoft.SharePoint.WebControls;

    namespace CopyOverItemFromOtherSP
    {

    {
    public string CopyOverFromOtherDomain(string userName, string Name, string userEmail,
    string userPhone, string listColumn1, string listColumn2, string listColumn3,
    string Comments)
    {
    Boolean done = false;

    /* Declare and initialize a variable for the Lists Web service. */
    sitesWebServiceLists.Lists listService = new sitesWebServiceLists.Lists();

    /* Authenticate the current user by passing their default credentials to the Web service from the system credential cache. */
    listService.Credentials = System.Net.CredentialCache.DefaultCredentials;

    /* Set the Url property of the service for the path to a subsite. */
    // Service URLs – MUST CHANGE TO POINT TO SPECIFIC SERVICE ENVIRONMENT
    // Change the web reference URL on the sitesWebServiceLists web service reference to be the address
    // that you are wanting to used based on the environment that you want to write to
    listService.Url = "<a href="#">http://sharepointtocopyitemto/_vti_bin/Lists.asmx</a>&quot;;

    /* Get Name attribute values (GUIDs) for list and view. */
    System.Xml.XmlNode ndListView = listService.GetListAndView("NameOfSharePointList", "");
    string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
    string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;

    /* Create an XmlDocument object and construct a Batch element and its
    attributes. Note that an empty ViewName parameter causes the method to use the default view. */
    System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
    System.Xml.XmlElement batchElement = doc.CreateElement("Batch");
    batchElement.SetAttribute("OnError", "Continue");
    batchElement.SetAttribute("ListVersion", "1");
    batchElement.SetAttribute("ViewName", strViewID);

    /* Specify methods for the batch post using CAML. To update or delete,
    specify the ID of the item, and to update or add, specify
    the value to place in the specified column. */
    batchElement.InnerXml = "<Method ID=’1′ Cmd=’New’>" +
    "<Field Name=’ID’>New</Field>" +
    "<Field Name=’Title’>" + userName.ToString() + "</Field>" +
    "<Field Name=’Name’>" + Name.ToString() + "</Field>" +
    "<Field Name=’userEmail’>" + userEmail.ToString() + "</Field>" +
    "<Field Name=’userPhone’>" + userPhone.ToString() + "</Field>" +
    "<Field Name=’listColumn1′>" + listColumn1.ToString() + "</Field>" +
    "<Field Name=’listColumn2′>" + listColumn2.ToString() + "</Field>" +
    "<Field Name=’listColumn3′>" + listColumn3.ToString() + "</Field>" +
    "<Field Name=’Comments’>" + Comments.ToString() + "</Field>" +
    "</Method>";

    /* Update list items. This example uses the list GUID, which is recommended,
    but the list display name will also work. */
    try
    {
    listService.UpdateListItems(strListID, batchElement);
    done = true;
    }
    catch
    {

    }
    if (done == true)
    {
    return done.ToString();
    }
    else
    {
    return done.ToString();
    }
    }
    }
    }
    [/sourcecode]

  • Programmatically Install A Root CA Certificate So Users Don’t Have To (C#)

    So here’s the backstory: I received the task of improving certificate enrollment so that users can

    1) Be verified via username, password, captcha & verification pin

    2) Auto Enroll without an external approval

    3) Simplify the process

    As some of you already know, ADCS via web enrollment is…how can we say…dated. So I wrote an application that sits in front of ADCS to first verify the user. Once they are through then the web enrollment is configured to let them run the wizard through to installing their cert. The issue that came to me is that most end-users will not take the time to ensure the root CA makes it to the trusted store (thus giving the classic CA cert is not installed message). So I received the order from on-high to “do it for them”. At first I struggled, attempting to understand how this could be done. I spoke with Microsoft and as I already was aware they indicated that having the user choose the trusted root store for the CA is by design. So what to do…ah I know, let’s just script it out.

    So this is as simple as it gets. Download the cert, store it on the local drive and use the built-in certmgr.exe to perform the root CA to trusted store installation. Here it is (this code is just one a basic console app):

    Code:
    [sourcecode]
    using System.Security.Cryptography.X509Certificates;

    WebClient webClient = new WebClient();
    webClient.DownloadFile("https://yourserver.domain.com/CertSrv/certnew.cer?ReqID=CACert&amp;Renewal=0&amp;Mode=inst&amp;Enc=b64&quot;, @"C:\Temp\certnew.cer");

    X509Store store = new X509Store(StoreName.Root,
    StoreLocation.LocalMachine);
    store.Open(OpenFlags.ReadWrite);
    X509Certificate2Collection collection = new X509Certificate2Collection();
    X509Certificate2 cert = new X509Certificate2(@"C:\Temp\certnew.cer");
    byte[] encodedCert = cert.GetRawCertData();
    Console.WriteLine("We are now installing the CA certificate into the Trusted Root Certificate store …");
    store.Add(cert);
    Console.WriteLine("Done! The CA certificate was successfully. Press any key to close.");
    Console.ReadKey();
    store.Close();
    [/sourcecode]
    This finishes the root CA portion so they can fly through the rest of web enrollment. Hope this helps.

  • Execute An Oracle Stored Procedure With Parameters (C#)

    So you want to execute an Oracle stored procedure with parameters, huh? For this example I have an Oracle stored procedure called MEMBER_TYPE_UPDATE that will update what type of membership I have based on the numeric value. This sort of snippet can be used in a web application directly or called by some form of web service.

    Here are some example values:

    0 = Not a member

    1 = Member

    2 = Member with first tier privileges

    3 = Member with highest level privileges

    The update occurs based on their username and based on that username will attempt to update the numeric value. So below we will be calling a method (passing the two parameters to update with). I will then gather the connection string to Oracle and begin to execute my Oracle stored procedure (while giving it the values passed into the method to be used in the stored procedure).

    Here’s my snippet:

    [sourcecode]
    using System;
    using System.Data;
    using System.Web.Services;
    using Oracle.DataAccess.Client;
    using System.Configuration;

    public string SetUserMembership(string membershipNetworkUserName, int membershipStatusValue)
    {
    string errorString = string.Empty;
    OracleCommand cmd = null;
    try
    {

    string connectionString = string.Empty;
    if (ConfigurationManager.AppSettings["location"].Contains("PROD"))
    {
    connectionString = ConfigurationManager.ConnectionStrings["ConnectionStringPROD"].ConnectionString;
    }
    else
    {
    connectionString = ConfigurationManager.ConnectionStrings["ConnectionStringDEV"].ConnectionString;
    }

    cmd = new OracleCommand();
    cmd.Connection = new OracleConnection(connectionString);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "ORACLE_USER.MEMBER_TYPE_UPDATE";
    cmd.Parameters.Add("in_employeeUserName", OracleDbType.Varchar2).Value = membershipNetworkUserName;
    cmd.Parameters.Add("in_status_id", OracleDbType.Decimal).Value = membershipStatusValue;
    cmd.Parameters.Add("O_RETURN_STATUS", OracleDbType.Varchar2, 4000).Direction = ParameterDirection.Output;
    cmd.Connection.Open();
    cmd.ExecuteNonQuery();

    string returnString = cmd.Parameters["O_RETURN_STATUS"].Value.ToString();
    if (!returnString.Contains("SUCCESS"))
    {
    // obviously there was an issue and we want to display this somewhere
    errorString = returnString;
    }
    }
    [/sourcecode]

    For a specific call I would call it with SetUserMembership(“username”, “2”);

    Ideally you would have variables there. Hope this helps people to see the parameters in use.