Minimal Authentication Methods

These are the minimal Methods I implemented:

public interface IIdentityChecker
{

[OperationContract]
string HelloWorld();

[OperationContract]
string AddUser(string theLogin, string thePassword);

[OperationContract]
Guid GetUserIdentifier(string theLogin, string thePassword);

}


In the actual implementation, we need to create these public methods.

public class IdentityBroker : IIdentityBroker
{

private static string myConnectionString = “server=TheDatabaseServer;database=AuthenticatorDB;UID=anAccountYouNeedToCreate;PWD=aPasswordyYouNeedToSet;”;

public string HelloWorld()
{

return “Hello World, Galaxy and Universe”;

}

}


As we are going to connect to a SQL database, add these references to your project:

using System.Data;
using System.Data.SqlClient;

We require a connection to the table that was created in: Create the Table, we will define a static string, to be used in the different methods.

Note that for production, this is later on to be moved out of the code, and put in the configuration file.

private static string myConnectionString = “server=TheDatabaseServer;database=AuthenticatorDB;UID=anAccount;PWD=aPassword;”;

Off course, you need to replace the generic servername, database, user and password to the ones you’re using in your setup 😉


This is the code for the 2 remaining methods:

AddUser

public string AddUser(string theLogin, string thePassword)
{

string anIdentifier;
try
{

using (SqlConnection MyConnection = new SqlConnection(myConnectionString))
{

using (SqlCommand cmd = new SqlCommand(“dbo.AddUser”, MyConnection))
{

cmd.CommandType = CommandType.StoredProcedure;

//Create and add a parameter to Parameters collection for the stored procedure.
cmd.Parameters.Add(new SqlParameter(“@NewLogin”, SqlDbType.NVarChar, 255));
cmd.Parameters.Add(new SqlParameter(“@NewPassword”, SqlDbType.NVarChar, 32));

//Create and add an output parameter to the Parameters collection.
cmd.Parameters.Add(new SqlParameter(“@Identifier”, SqlDbType.UniqueIdentifier));
cmd.Parameters.Add(new SqlParameter(“@ResponseMessage”, SqlDbType.NVarChar, 128));

//Set the direction for the parameter.
cmd.Parameters[“@Identifier”].Direction = ParameterDirection.Output;
cmd.Parameters[“@ResponseMessage”].Direction = ParameterDirection.Output;

//Assign the input values to the parameter.
cmd.Parameters[“@NewLogin”].Value = (theLogin).Trim();
cmd.Parameters[“@NewPassword”].Value = (thePassword).Trim();
MyConnection.Open();
cmd.ExecuteNonQuery();

anIdentifier = cmd.Parameters[“@Identifier”].Value.ToString();

}

}

}

catch (Exception ex)
{

anIdentifier = “”;

}

return anIdentifier;

}


 

GetUserIdentifier

public Guid GetUserIdentifier(string theLogin, string thePassword)
{

Guid anIdentifier;
try
{

using (SqlConnection MyConnection = new SqlConnection(myConnectionString))
{

using (SqlCommand cmd = new SqlCommand(“dbo.GetUserIdentifier”, MyConnection))
{

cmd.CommandType = CommandType.StoredProcedure;

//Create and add a parameter to Parameters collection for the stored procedure.
cmd.Parameters.Add(new SqlParameter(“@TheLogin”, SqlDbType.NVarChar, 255));
cmd.Parameters.Add(new SqlParameter(“@ThePassword”, SqlDbType.NVarChar, 32));

//Create and add an output parameter to the Parameters collection.
cmd.Parameters.Add(new SqlParameter(“@Identifier”, SqlDbType.UniqueIdentifier));
cmd.Parameters.Add(new SqlParameter(“@ResponseMessage”, SqlDbType.NVarChar, 128));

//Set the direction for the parameter.
cmd.Parameters[“@Identifier”].Direction = ParameterDirection.Output;
cmd.Parameters[“@ResponseMessage”].Direction = ParameterDirection.Output;

//Assign the input values to the parameter.
cmd.Parameters[“@TheLogin”].Value = (theLogin).Trim();
cmd.Parameters[“@ThePassword”].Value = (thePassword).Trim();
MyConnection.Open();
cmd.ExecuteNonQuery();

anIdentifier = new Guid(cmd.Parameters[“@Identifier”].Value.ToString());

}

}

}

catch (Exception ex)

{

anIdentifier = Guid.Empty;

}

return anIdentifier;

}

 

 

 

 

 

 

 

One thought on “Minimal Authentication Methods

Post a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s