Feed on
Posts
Comments

X509 Certificate Installation

Here are the steps of installing an X509 certificate in windows:

Installation Steps for DEVAssumptions:

  • The Microsoft Windows SDK exists on the machine
  • The certificate name is SomeName

  1. Go to Start > All Programs > Microsoft Windows SDK > CMD Shell
  2. Execute the following command: makecert -r -pe -n “CN=SomeName” -b 01/01/2000 -e 01/01/2036 -eku 1.3.6.1.5.5.7.3.1 -ss my -sr LocalMachine -sky exchange -sp “Microsoft RSA SChannel Cryptographic Provider” -sy 12
  3. Copy the executable findprivatekey.exe to an arbitrary folder
  4. Go to Start > Run type cmd
  5. Browse to the folder where the findprivatekey.exe was copied
  6. Execute the following command: findprivatekey.exe My LocalMachine -n “CN=SomeName” -a
  7. Copy the output of that command in Notepad
  8. Remove all line breaks if you see any
  9. Replace the {output from above} by the text from step 8 and execute the following command:  cacls “{output from above}” /E /P NETWORKSERVICE:R Example: cacls “C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys\9e7f481ca4127144bc75102dabb32ad0_c18e0de9-0e80-4436-920c-4ab1cae7939a” /E /P NETWORKSERVICE:R
  10. You’re done!

 Installation Steps for QA/ProdAssumptions:

  • The Microsoft Windows SDK exists on the machine
  • The certificate name is SomeName and is present in the local machine maybe with a different file name
  1. Go to Start > Run
  2. Type mmc
  3. Click on File > Add/Remove Snap-in…
  4. Click the Add
  5. Click on Certificates
  6. Select Computer Account
  7. Click Finish
  8. Click Close to finish adding the Snap-in
  9. Click Ok
  10. Under Console Root, expand Certificates (Local Computer)
  11. Expand Personal
  12. Click Certificates
  13. Right click and click on All Tasks > Import
  14. Click Next
  15. Click Browse, locate the exported certificate (use *.*) and then double click on it
  16. Click Next
  17. Type the password and then click Next
  18. Select Place all certificates in the following store
  19. If Personal isn’t showing in the box under, then click Browse, select Personal and click OK
  20. Click Next
  21. Click Finish
  22. Click OK
  23. Copy the executable findprivatekey.exe to c:\
  24. Go to Start > Run type cmd
  25. Go to the C:\ prompt
  26. Execute the following command: findprivatekey.exe My LocalMachine -n “CN=SomeName” -a
  27. Copy the output of that command in Notepad
  28. Remove all line breaks if you see any
  29. Replace the {output from above} by the text from step 29 and execute the following command:  cacls “{output from above}” /E /P NETWORKSERVICE:R Example: cacls “C:\Documents and Settings\All Users\Application Data\Microsoft\Crypto\RSA\MachineKeys\9e7f481ca4127144bc75102dabb32ad0_c18e0de9-0e80-4436-920c-4ab1cae7939a” /E /P NETWORKSERVICE:R
  30. You’re done!

The findprivatekey.exe is available here (rename .gif to .exe)

Reusable Data Layer In C#

The goal here is to take advantage of C# 2.0 features as generics and create a reusable data layer capable of calling any stored procedure, with any sets of parameters and returning different sets of results.

The DataUtility class:


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;

///
/// Summary description for DataUtility
///
namespace Data
{
    public class DataUtility : Common
    {
        private string conStr;

        private enum ExecuteType
        {
            NonQuery,
            Scalar,
            Reader,
            DataRow,
            DataTable,
            DataSet
        }

        public DataUtility(string conStr)
        {
            this.conStr = conStr;
        }

        public Result ExecuteNonQuery(string storedProcedureName, Hashtable parameters)
        {
            object returnValue;
            return Execute(out returnValue, storedProcedureName, parameters, ExecuteType.NonQuery);
        }

        public Result ExecuteScalar(out object scalar, string storedProcedureName, Hashtable parameters)
        {
            return Execute(out scalar, storedProcedureName, parameters, ExecuteType.Scalar);
        }

        public Result ExecuteReader(out SqlDataReader reader, string storedProcedureName, Hashtable parameters)
        {
            return Execute(out reader, storedProcedureName, parameters, ExecuteType.Reader);
        }

        public Result ExecuteDataRow(out DataRow dataRow, string storedProcedureName, Hashtable parameters)
        {
            return Execute(out dataRow, storedProcedureName, parameters, ExecuteType.DataRow);
        }

        public Result ExecuteDataTable(out DataTable dataTable, string storedProcedureName, Hashtable parameters)
        {
            return Execute(out dataTable, storedProcedureName, parameters, ExecuteType.DataTable);
        }

        public Result ExecuteDataSet(out DataSet dataSet, string storedProcedureName, Hashtable parameters)
        {
            return Execute(out dataSet, storedProcedureName, parameters, ExecuteType.DataSet);
        }

        private Result Execute(out T returnValue, string storedProcedureName, Hashtable parameters, ExecuteType executeType) where T : class
        {
            Result result = new Result();
            returnValue = null;

            try
            {
                using (SqlConnection connection = new SqlConnection(conStr))
                {
                    SqlCommand command = new SqlCommand(storedProcedureName, connection);
                    command.CommandType = CommandType.StoredProcedure;
                    command.Connection.Open();

                    foreach (string name in parameters.Keys)
                    {
                        command.Parameters.AddWithValue(name, parameters[name]);
                    }

                    SqlDataAdapter adapter;
                    DataTable dataTable;

                    switch (executeType)
                    {
                        case ExecuteType.NonQuery:
                            command.ExecuteNonQuery();
                            break;

                        case ExecuteType.Scalar:
                            returnValue = command.ExecuteScalar() as T;
                            break;

                        case ExecuteType.Reader:
                            returnValue = command.ExecuteReader() as T;
                            break;

                        case ExecuteType.DataRow:
                            adapter = new SqlDataAdapter(command);
                            dataTable = new DataTable();
                            adapter.Fill(dataTable);

                            if (dataTable.Rows.Count > 0)
                            {
                                returnValue = dataTable.Rows[0] as T;
                            }

                            break;

                        case ExecuteType.DataTable:
                            adapter = new SqlDataAdapter(command);
                            dataTable = new DataTable();
                            adapter.Fill(dataTable);
                            returnValue = dataTable as T;
                            break;

                        case ExecuteType.DataSet:
                            adapter = new SqlDataAdapter(command);
                            DataSet dataSet = new DataSet();
                            adapter.Fill(dataSet);
                            returnValue = dataSet as T;
                            break;
                    }

                    connection.Close();
                }
            }
            catch (Exception exception)
            {
                result.ResultType = ResultType.Failure;
                result.Exception = exception;
            }

            return result;
        }
    }
}

The Result class:


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

///
/// Summary description for Result
///
namespace Data
{
    public class Result : Common
    {
        private ResultType resultType = ResultType.Success;
        private Exception exception = null;

        public Result()
        {
        }

        public Result(ResultType resultType, Exception exception)
        {
            this.resultType = resultType;
            this.exception = exception;
        }

        public Result(ResultType resultType)
        {
            this.resultType = resultType;
        }

        public ResultType ResultType
        {
            get { return resultType; }
            set { resultType = value; }
        }

        public Exception Exception
        {
            get { return exception; }
            set { exception = value; }
        }
    }
}

The Common class


using System;
using System.Collections.Generic;
using System.Text;

namespace Data
{
    public class Common
    {
        public enum ResultType
        {
            Success,
            Failure
        }
    }
}

Example of use:


        public User SignIn(string username, string password)
        {
            User user = null;
            Hashtable parameters = new Hashtable();
            parameters.Add("Name", username);
            parameters.Add("Password", password);
            DataRow dr;

            if ((dataUtility.ExecuteDataRow(out dr, "UserSelect", parameters).ResultType == ResultType.Success) && (dr != null))
            {
                user = new User
                    (
                        Convert.ToInt32(dr["ID"]),
                        dr["Name"].ToString(),
                        dr["Password"].ToString(),
                        dr["Email"].ToString(),
                        dr["Question"].ToString(),
                        dr["Answer"].ToString()
                    );
            }

            return user;
        }

Sometimes, there is a need to post a form from one page to another without any UI element. This is how to do it programmatically:

ASCIIEncoding encoding = new ASCIIEncoding();

byte[] data = encoding.GetBytes(“FormElementName=” + formElementValue);

HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(http://somesite/somepage);

myRequest.Method = “POST”;

myRequest.ContentType = “application/x-www-form-urlencoded”;

myRequest.ContentLength = data.Length;

Stream newStream = myRequest.GetRequestStream();

newStream.Write(data, 0, data.Length);

newStream.Close();

<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head runat=”server”>

<title>Untitled Page</title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>

<asp:ListBox ID=”LBSource” SelectionMode=”Multiple” runat=”server”>

  <asp:ListItem Text=”Item1″ Value=”vItem1″></asp:ListItem>

  <asp:ListItem Text=”Item2″ Value=”vItem2″></asp:ListItem>

  <asp:ListItem Text=”Item3″ Value=”vItem3″></asp:ListItem>

  <asp:ListItem Text=”Item4″ Value=”vItem4″></asp:ListItem>

  <asp:ListItem Text=”Item5″ Value=”vItem5″></asp:ListItem>

</asp:ListBox>

<input type=”button” value=”Add” onclick=”moveItems(’<%= LBSource.ClientID %>‘, ‘<%= LBDestination.ClientID %>‘)” />

<input type=”button” value=”Remove” onclick=”moveItems(’<%= LBDestination.ClientID %>‘, ‘<%= LBSource.ClientID %>‘)” />

<asp:ListBox ID=”LBDestination” SelectionMode=”Multiple” runat=”server”></asp:ListBox>

</div>

</form>

<script language=”javascript” type=”text/javascript”>

function moveItems(srcID, dstID)

{

  src = document.getElementById(srcID);

  dst = document.getElementById(dstID); var found = true;

  while (found)

  {

    found = false;

    for (var i = 0; i < src.length; i++)

    {

      if (src[i].selected)

      {

        dst.appendChild(src[i].cloneNode(true));

        src.removeChild(src[i]);

        found = true;

        break;

      }

    }

  }

}

</script>

</body>

</html>

Case Statement In SQL

SELECT SomeColumn1 =

(

  SELECT

    CASE

      WHEN SomeColumn3 IS NULL THEN ISNULL(SomeColumn3,)

      ELSE ISNULL(SomeColumn4,)

    END

  FROM SomeTable2 WHERE SomeColumn5 = @SomeParameter2

)

FROM SomeTable1

WHERE SomeColumn2 = @SomeParameter1

INSERT SomeTable

(

  @SomeColumn1,

  @SomeColumn2

)

VALUES

(

  @SomeParameter1,

  @SomeParameter2

)

SELECT IDENT_CURRENT(‘SomeTable’)

Creating A Temp Table In SQL

CREATE TABLE #TempTable

( 

  SomeColumn1 INT,

  SomeColumn2 VARCHAR(50)

)

INSERT INTO #TempTable

SELECT SomeColumn3, SomeColumn4 FROM SomeTable WHERE SomeColumn5 = @SomeParameter

DROP TABLE #TempTable

DECLARE @Value

INSERT @Value = (SELECT SomeColumn1 FROM SomeTable WHERE SomeColumn2 = @SomeParameter)

Interview Questions

C#

1. What is an abstract class?
2. What is the difference between an abstract class and an interface?
3. What is the access level for methods in an interface?
4. What is the difference between value types and reference types?
5. Is struct a value type or a reference type?
6. Is it possible to add a method to a struct?

    public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
    {
        // Check if the target directory exists, if not, create it.
        if (Directory.Exists(target.FullName) == false)
        {
            Directory.CreateDirectory(target.FullName);
        }

        // Copy each file into it’s new directory.
        foreach (FileInfo fi in source.GetFiles())
        {
            Console.WriteLine(@”Copying {0}\{1}”, target.FullName, fi.Name);
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
        }

        // Copy each subdirectory using recursion.
        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
        {
            DirectoryInfo nextTargetSubDir =
                target.CreateSubdirectory(diSourceSubDir.Name);
            CopyAll(diSourceSubDir, nextTargetSubDir);
        }
    }

« Newer Posts - Older Posts »