473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

I need help with something

Ok let me try to explain this as good as I can. I am creating this
application where it contains a userlogin class. The user logs in
before entering the main apploication. I want to do audit trails and I
am trying to figure out how to get the user ID that logged in. Can
anyone help me.

I have samples if anyone needs them.

Oct 1 '06 #1
11 1830
Hi Matthew,

Where does the userlogin class get the user's data from?
And what do you mean by user ID? Are you referring to a database column or web service return value, for example?
Or are you just looking for a way to identify the Windows user that logged into your ASP.NET program?
Perhaps you're running a WinForms application that uses LDAP to login its users?

You'll need to supply these answers, at least, if anyone is going to be able to help you.

See Jon's Skeets article: http://www.yoda.arachsys.com/csharp/complete.html
I have samples if anyone needs them.
Might be useful. You'll have to supply more information about the problem at hand if anyone is going to determine the usefulness of
examples.

--
Dave Sexton

"Matthew" <ma******@yahoo.comwrote in message news:11**********************@m73g2000cwd.googlegr oups.com...
Ok let me try to explain this as good as I can. I am creating this
application where it contains a userlogin class. The user logs in
before entering the main apploication. I want to do audit trails and I
am trying to figure out how to get the user ID that logged in. Can
anyone help me.

I have samples if anyone needs them.

Oct 1 '06 #2
Hey Dave,

This is a windows application first and foremost and I am using C#.
the userlogin class is a seperate class by itself, the ID is one of the
public properties of the userlogin class.

The logic goes like this.

The application starts up and the login screen loads up. The user
enters their ID and password, and presses llogin button, where the
userlogin class does the authentication. and if the authentication
passes then the login screen unloads and themain screen loads.
USERLOGIN CLASS:

namespace RegIT.RegitClasses
{
/// <summary>
/// Summary description for UserLogin.
/// </summary>
///

public class regitUserLogin
{
private int uloginID;
private string uloginEmpNumber;
private string uloginPassword;
private string uloginCreateDate;
private string uloginChangeDate;
private string uloginChangeBy;
private regitUserLogin[] uloginList;
private string strEncrKey = "m74r95c96=";
public string PassWordHash;

public int ID
{
get{return uloginID;}
set{uloginID = value;}
}
public string EmpNumber
{
get{return uloginEmpNumber;}
set{uloginEmpNumber = value;}
}
public string Password
{
get{return uloginPassword;}
set
{
uloginPassword = value;
uloginPassword = EncryptedPassword;
}
}
public string CreateDate
{
get{return uloginCreateDate;}
set{uloginCreateDate = Convert.ToString(value);}
}
public string ChangeDate
{
get{return uloginChangeDate;}
set{uloginChangeDate = Convert.ToString(value);}
}
public string ChangeBy
{
get{return uloginChangeBy;}
set{uloginChangeBy = value;}
}
public regitUserLogin[] LoginList
{
get{return uloginList;}
set{uloginList=value;}
}
private string EncryptedPassword
{

get{return Encrypt(strEncrKey, out uloginPassword);}
}

protected string Encrypt(string EncryptionKey,out string
uloginPassword)
{
byte[] byteKey = Encoding.UTF8.GetBytes(EncryptionKey);
string regitPWD = Password;
HMACSHA1 hmac = new HMACSHA1(byteKey);

byte[] bytePWD = Encoding.UTF8.GetBytes(regitPWD);
byte[] byteHash = hmac.ComputeHash(bytePWD);
uloginPassword = Convert.ToBase64String(byteHash);

return uloginPassword;
}

public bool regitAuthenticateLogin(string EmpNumber, string Password)
{
regitDatabase regitDB = new regitDatabase();
SqlParameter[] regitParams = new SqlParameter[2];
SqlDataReader regitDR = null;
try
{
string compareENum = "";
string compareEPwd = "";

regitParams[0] = regitDB.regitMakeParameter("@EmpNumber",
Convert.ToString(EmpNumber).Trim());
regitParams[1] = regitDB.regitMakeParameter("@EmpPassword",
Convert.ToString(Password).Trim());
regitDB.regitRunProcedure("regit_sp_Get_UserLogin_ Authentication",
regitParams, ref regitDR);

while(regitDR.Read())
{
compareENum = regitDR["emp_Number"].ToString();
compareEPwd = regitDR["emp_Password"].ToString();
}
regitDR.Close();
regitDB.regitConnectionClose();
regitDB.regitConnectionDispose();

if(compareENum==EmpNumber && compareEPwd==Password)
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
throw(ex);
return false;
}
finally
{
regitDR.Close();
regitDB.regitConnectionClose();
regitDB.regitConnectionDispose();
regitParams = null;
}
}
public bool regitCreateLogin(string EmpNumber, string Password)
{
regitDatabase regitDB = new regitDatabase();
SqlParameter[] regitParams = new SqlParameter[2];

try
{
regitParams[0] = regitDB.regitMakeParameter("@EmpNumber",
EmpNumber);
regitParams[1] = regitDB.regitMakeParameter("@EmpPassword",
Password);
regitDB.regitRunProcedure("regit_sp_Add_UserLogin" , regitParams);

return true;
}
catch(SqlException sqlex)
{
throw(sqlex);
}
catch
{return false;}
finally
{
regitParams = null;
regitDB.regitConnectionClose();
regitDB.regitConnectionDispose();
}
}
}
}

Oct 1 '06 #3
Hi Matthew,

Your "logic" sounds fine but you haven't asked any questions yet. What do you need help with?

--
Dave Sexton

"Matthew" <ma******@yahoo.comwrote in message news:11**********************@m7g2000cwm.googlegro ups.com...
Hey Dave,

This is a windows application first and foremost and I am using C#.
the userlogin class is a seperate class by itself, the ID is one of the
public properties of the userlogin class.

The logic goes like this.

The application starts up and the login screen loads up. The user
enters their ID and password, and presses llogin button, where the
userlogin class does the authentication. and if the authentication
passes then the login screen unloads and themain screen loads.
USERLOGIN CLASS:

namespace RegIT.RegitClasses
{
/// <summary>
/// Summary description for UserLogin.
/// </summary>
///

public class regitUserLogin
{
private int uloginID;
private string uloginEmpNumber;
private string uloginPassword;
private string uloginCreateDate;
private string uloginChangeDate;
private string uloginChangeBy;
private regitUserLogin[] uloginList;
private string strEncrKey = "m74r95c96=";
public string PassWordHash;

public int ID
{
get{return uloginID;}
set{uloginID = value;}
}
public string EmpNumber
{
get{return uloginEmpNumber;}
set{uloginEmpNumber = value;}
}
public string Password
{
get{return uloginPassword;}
set
{
uloginPassword = value;
uloginPassword = EncryptedPassword;
}
}
public string CreateDate
{
get{return uloginCreateDate;}
set{uloginCreateDate = Convert.ToString(value);}
}
public string ChangeDate
{
get{return uloginChangeDate;}
set{uloginChangeDate = Convert.ToString(value);}
}
public string ChangeBy
{
get{return uloginChangeBy;}
set{uloginChangeBy = value;}
}
public regitUserLogin[] LoginList
{
get{return uloginList;}
set{uloginList=value;}
}
private string EncryptedPassword
{

get{return Encrypt(strEncrKey, out uloginPassword);}
}

protected string Encrypt(string EncryptionKey,out string
uloginPassword)
{
byte[] byteKey = Encoding.UTF8.GetBytes(EncryptionKey);
string regitPWD = Password;
HMACSHA1 hmac = new HMACSHA1(byteKey);

byte[] bytePWD = Encoding.UTF8.GetBytes(regitPWD);
byte[] byteHash = hmac.ComputeHash(bytePWD);
uloginPassword = Convert.ToBase64String(byteHash);

return uloginPassword;
}

public bool regitAuthenticateLogin(string EmpNumber, string Password)
{
regitDatabase regitDB = new regitDatabase();
SqlParameter[] regitParams = new SqlParameter[2];
SqlDataReader regitDR = null;
try
{
string compareENum = "";
string compareEPwd = "";

regitParams[0] = regitDB.regitMakeParameter("@EmpNumber",
Convert.ToString(EmpNumber).Trim());
regitParams[1] = regitDB.regitMakeParameter("@EmpPassword",
Convert.ToString(Password).Trim());
regitDB.regitRunProcedure("regit_sp_Get_UserLogin_ Authentication",
regitParams, ref regitDR);

while(regitDR.Read())
{
compareENum = regitDR["emp_Number"].ToString();
compareEPwd = regitDR["emp_Password"].ToString();
}
regitDR.Close();
regitDB.regitConnectionClose();
regitDB.regitConnectionDispose();

if(compareENum==EmpNumber && compareEPwd==Password)
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
throw(ex);
return false;
}
finally
{
regitDR.Close();
regitDB.regitConnectionClose();
regitDB.regitConnectionDispose();
regitParams = null;
}
}
public bool regitCreateLogin(string EmpNumber, string Password)
{
regitDatabase regitDB = new regitDatabase();
SqlParameter[] regitParams = new SqlParameter[2];

try
{
regitParams[0] = regitDB.regitMakeParameter("@EmpNumber",
EmpNumber);
regitParams[1] = regitDB.regitMakeParameter("@EmpPassword",
Password);
regitDB.regitRunProcedure("regit_sp_Add_UserLogin" , regitParams);

return true;
}
catch(SqlException sqlex)
{
throw(sqlex);
}
catch
{return false;}
finally
{
regitParams = null;
regitDB.regitConnectionClose();
regitDB.regitConnectionDispose();
}
}
}
}

Oct 1 '06 #4
Sorry Dave,

Ok the question is, how do I go about setting a global isntance of the
a user class in which I can call anytime to retrieve the user that is
logged in into the application. I know this should be something I
should know, but I am used to VB, and learning C# by implementing it
into my own application, basically trying to learn it on my own.

Oct 1 '06 #5
Hi Matthew,

There are a few ways to accomplish your goal but I've included the simplest example I could think of here:

internal sealed class User
{
/// <summary>Gets the currently logged in <see cref="User" /or <c>null</c>.</summary>
public static User LoginUser { get { return loginUser; } } // note the "static" keyword

// instance properties (not "shared")
public string UserName { get { return userName; } }
public string Password { get { return password; } }

// private, "shared" reference to the currently logged in user
private static User loginUser; // note the "static" keyword

// private instance (not "shared") fields
private string userName, password;

// using a single, private constructor prevents instances of this class from being created externally (by other classes other
than User itself)
private User()
{
}

/// <summary>Logs in a <see cref="User" /with the specified <paramref name="userName" /and <paramref name="password"
/>.</summary>
/// <param name="userName">Name of the user to be logged in.</param>
/// <param name="password">Password of the user to be logged in.</param>
public static User Login(string userName, string password)
{
if (loginUser != null)
throw new InvalidOperationException("A user is already logged into the application: " + loginUser.UserName);

User user = new User();
user.userName = userName;
user.password = password;

// TODO: login user with supplied credentials

// store user in static field for "shared" access
loginUser = user;
return user;
}
}
The User class can be used as such:

// Login the user using the static Login method:
User user = User.Login("user name", "the password");

// Later, in code where you don't have that user variable (it is out of scope)
// the logged in User can be retrieve through the static LoginUser property:
User user = User.LoginUser;
string loginUserName = user.UserName;
(Please note that I didn't try to build this code. If you have any problems building it, or understanding it for that matter, then
just let me know and I'll try to help)

I used the static keyword on the loginUser field and the LoginUser property so that the logged in User can be referenced in code
without the need of a User instance. I believe VB used "Modules" for this type of functionality, however Modules have a global
visibility, IIRC. In C# you can only access the static members above by explicitly referencing the User class: User.Login("name",
"password") and User.LoginUser, as in my code sample.

(Note: I seem to remember that shared was a common VB term, so I laced the comments above with the term "shared" to make things
clearer to you, however I recommend that you get used to using the term "static" instead if you aren't already. ;)

Another common way to retrieve the login user is to create an IIdentity implementation (yes, with two I's) and add it to a new
GenericPrincipal instance when the user first logs in. Then, assign the principal to the current Thread via the static
Thread.CurrentPrincipal property. It can be retrieved at anytime by any code that executes on that Thread. (see the
System.Security.Principal namespace and the System.Threading namespace).

--
Dave Sexton

"Matthew" <ma******@yahoo.comwrote in message news:11**********************@b28g2000cwb.googlegr oups.com...
Sorry Dave,

Ok the question is, how do I go about setting a global isntance of the
a user class in which I can call anytime to retrieve the user that is
logged in into the application. I know this should be something I
should know, but I am used to VB, and learning C# by implementing it
into my own application, basically trying to learn it on my own.

Oct 1 '06 #6
Thanks Dave,

I am going to implement that into the code and build it and see if it
works to what I need. You know what you are talking about so thank you
very much in advanced.

I will let you know how it turns out.

Matthew

Oct 1 '06 #7
Thanks Dave,

I am going to implement that into the code and build it and see if it
works to what I need. You know what you are talking about so thank you
very much in advanced.

I will let you know how it turns out.

Matthew

Oct 1 '06 #8
Thanks Dave,

I am going to implement that into the code and build it and see if it
works to what I need. You know what you are talking about so thank you
very much in advanced.

I will let you know how it turns out.

Matthew

Oct 1 '06 #9
Dave,

where do I place the internal sealed class User. I tried to place it
into its own class file but I can not access it dues to the privileges
and protection level.

Oct 1 '06 #10
Hi Matthew,

You can change internal to public if you'd like. I try to mark all classes that will not be used externally as internal and I just
assumed that your login code would be internal to the assembly in which it was coded. Of course, if you are declaring the class in
a class library and referencing it in from within a different project then it will have to be public, not internal.

--
Dave Sexton

"Matthew" <ma******@yahoo.comwrote in message news:11**********************@h48g2000cwc.googlegr oups.com...
Dave,

where do I place the internal sealed class User. I tried to place it
into its own class file but I can not access it dues to the privileges
and protection level.

Oct 1 '06 #11
Thanks Dave once again.

Oct 1 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: has | last post by:
<BLUSH> Careless talk costs lives, as they say. In my case, a throwaway comment that Python could trounce the notoriously underpowered and undersupported AppleScript language for "serious number...
7
by: Mike Kamermans | last post by:
I hope someone can help me, because what I'm going through at the moment trying to edit XML documents is enough to make me want to never edit XML again. I'm looking for an XML editor that has a...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
19
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
5
by: Confused User | last post by:
I am working on device that utilizes a Motorola 68HC16 microcontroller. I am using an old unsupported piece of crap Whitesmith's / Intermetrics / Tasking compiler. The embedded compiler business...
7
by: Jack Addington | last post by:
I've got a fairly simple application implementation that over time is going to get a lot bigger. I'm really trying to implement it in a way that will facilitate the growth. I am first writing a...
18
by: Q. John Chen | last post by:
I have Vidation Controls First One: Simple exluce certain special characters: say no a or b or c in the string: * Second One: I required date be entered in "MM/DD/YYYY" format: //+4 How...
8
by: Bruno Alexandre | last post by:
Hi guys, I'm using a session to save an ArrayList, so I do not read Database everytime user reload the page or enter the site (the Data is consistent for all entire session time when the user is...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
7
by: The Cool Giraffe | last post by:
Please note that i do intend to use a header file. However, i'm not sure if it's really needed or just a convention. Suppose we have the following two files. // Something.h class Something {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.