Part 8 Check If The Request Is From A Valid User
Create the class that will check if the user is currently allowed on the system.
Once again, this example uses the AresAdmin account used in the ChangePassword.cs class.
The AresAdmin account will need read permission to whatever database system you use to record your users.
Create a SQL view that retrieves the details for the username and the MiFare details from the user’s card. This view can be expanded to include other information such as name or active directory group.
This example sets Users as the OU in Active Directory where the account will be created.
Copy the method used to create the Globals.cs class but this time call it ‘CheckIfActive.cs’.
Change the code inside CheckIfActive.cs to the following:
using System;
using System.Data;
using System.Data.SqlClient;
namespace Ares
{
class CheckIfActive
{
public string IsActive(string CardReaderValue)
{
try
{
string IsUserActive = “No”;
//Set up the connection using the given credentials.
SqlConnection connection = new SqlConnection(“Server=DatabaseServer;Database=DatabaseName;UID=AcreAdmin;PWD=123456789012345;”);
SqlCommand commandLoading = new SqlCommand();
commandLoading.Connection = connection;
connection.Open();
commandLoading.CommandType = CommandType.Text;
commandLoading.CommandText = “SELECT * FROM <database name>.<view name> WHERE <view name>.<field name used for MiFare> = ‘” + CardReaderValue + “‘”;
DataSet datasetLoading = new DataSet();
SqlDataAdapter adapterLoading = new SqlDataAdapter(commandLoading);
adapterLoading.Fill(datasetLoading);
if (datasetLoading.Tables[0].Rows.Count == 0)
{
//No user exists in the database for this card
IsUserActive = “No”;
}
else
{
//Is an active user
IsUserActive = “Yes”;
//Assign User Info for use later
Globals.accountname = datasetLoading.Tables[0].Rows[0][“<username field>”].ToString();
Globals.miFare = CardReaderValue;
Globals.homeDir = “<network location where you store user home folders>” + Globals.accountname;
Globals.profileDir = “<network location where you store profiles>” + Globals.accountname;
Globals.exchangeDB = “<Exchange Database Name”;
Globals.ExchPolicy = “<Name of the Exchange policy that you wish to use>”;
Globals.cnValue = “CN=” + Globals.accountname + “,OU=Users,DC=<Domain Name>,DC=<Domain Name>”;
Globals.cnSite = “OU=Users,DC=<Domain Name>,DC=<Domain Name>”;
}
return IsUserActive;
}
catch (Exception e)
{
Globals.errorCode = e.ToString() + ” – Error in SQL – If Active”;
return “No”;
}
}
}
}