473,654 Members | 3,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1851
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.goo glegroups.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.RegitClas ses
{
/// <summary>
/// Summary description for UserLogin.
/// </summary>
///

public class regitUserLogin
{
private int uloginID;
private string uloginEmpNumber ;
private string uloginPassword;
private string uloginCreateDat e;
private string uloginChangeDat e;
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{uloginEmpNu mber = value;}
}
public string Password
{
get{return uloginPassword; }
set
{
uloginPassword = value;
uloginPassword = EncryptedPasswo rd;
}
}
public string CreateDate
{
get{return uloginCreateDat e;}
set{uloginCreat eDate = Convert.ToStrin g(value);}
}
public string ChangeDate
{
get{return uloginChangeDat e;}
set{uloginChang eDate = Convert.ToStrin g(value);}
}
public string ChangeBy
{
get{return uloginChangeBy; }
set{uloginChang eBy = value;}
}
public regitUserLogin[] LoginList
{
get{return uloginList;}
set{uloginList= value;}
}
private string EncryptedPasswo rd
{

get{return Encrypt(strEncr Key, out uloginPassword) ;}
}

protected string Encrypt(string EncryptionKey,o ut string
uloginPassword)
{
byte[] byteKey = Encoding.UTF8.G etBytes(Encrypt ionKey);
string regitPWD = Password;
HMACSHA1 hmac = new HMACSHA1(byteKe y);

byte[] bytePWD = Encoding.UTF8.G etBytes(regitPW D);
byte[] byteHash = hmac.ComputeHas h(bytePWD);
uloginPassword = Convert.ToBase6 4String(byteHas h);

return uloginPassword;
}

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

regitParams[0] = regitDB.regitMa keParameter("@E mpNumber",
Convert.ToStrin g(EmpNumber).Tr im());
regitParams[1] = regitDB.regitMa keParameter("@E mpPassword",
Convert.ToStrin g(Password).Tri m());
regitDB.regitRu nProcedure("reg it_sp_Get_UserL ogin_Authentica tion",
regitParams, ref regitDR);

while(regitDR.R ead())
{
compareENum = regitDR["emp_Number "].ToString();
compareEPwd = regitDR["emp_Passwo rd"].ToString();
}
regitDR.Close() ;
regitDB.regitCo nnectionClose() ;
regitDB.regitCo nnectionDispose ();

if(compareENum= =EmpNumber && compareEPwd==Pa ssword)
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
throw(ex);
return false;
}
finally
{
regitDR.Close() ;
regitDB.regitCo nnectionClose() ;
regitDB.regitCo nnectionDispose ();
regitParams = null;
}
}
public bool regitCreateLogi n(string EmpNumber, string Password)
{
regitDatabase regitDB = new regitDatabase() ;
SqlParameter[] regitParams = new SqlParameter[2];

try
{
regitParams[0] = regitDB.regitMa keParameter("@E mpNumber",
EmpNumber);
regitParams[1] = regitDB.regitMa keParameter("@E mpPassword",
Password);
regitDB.regitRu nProcedure("reg it_sp_Add_UserL ogin", regitParams);

return true;
}
catch(SqlExcept ion sqlex)
{
throw(sqlex);
}
catch
{return false;}
finally
{
regitParams = null;
regitDB.regitCo nnectionClose() ;
regitDB.regitCo nnectionDispose ();
}
}
}
}

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.goog legroups.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.RegitClas ses
{
/// <summary>
/// Summary description for UserLogin.
/// </summary>
///

public class regitUserLogin
{
private int uloginID;
private string uloginEmpNumber ;
private string uloginPassword;
private string uloginCreateDat e;
private string uloginChangeDat e;
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{uloginEmpNu mber = value;}
}
public string Password
{
get{return uloginPassword; }
set
{
uloginPassword = value;
uloginPassword = EncryptedPasswo rd;
}
}
public string CreateDate
{
get{return uloginCreateDat e;}
set{uloginCreat eDate = Convert.ToStrin g(value);}
}
public string ChangeDate
{
get{return uloginChangeDat e;}
set{uloginChang eDate = Convert.ToStrin g(value);}
}
public string ChangeBy
{
get{return uloginChangeBy; }
set{uloginChang eBy = value;}
}
public regitUserLogin[] LoginList
{
get{return uloginList;}
set{uloginList= value;}
}
private string EncryptedPasswo rd
{

get{return Encrypt(strEncr Key, out uloginPassword) ;}
}

protected string Encrypt(string EncryptionKey,o ut string
uloginPassword)
{
byte[] byteKey = Encoding.UTF8.G etBytes(Encrypt ionKey);
string regitPWD = Password;
HMACSHA1 hmac = new HMACSHA1(byteKe y);

byte[] bytePWD = Encoding.UTF8.G etBytes(regitPW D);
byte[] byteHash = hmac.ComputeHas h(bytePWD);
uloginPassword = Convert.ToBase6 4String(byteHas h);

return uloginPassword;
}

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

regitParams[0] = regitDB.regitMa keParameter("@E mpNumber",
Convert.ToStrin g(EmpNumber).Tr im());
regitParams[1] = regitDB.regitMa keParameter("@E mpPassword",
Convert.ToStrin g(Password).Tri m());
regitDB.regitRu nProcedure("reg it_sp_Get_UserL ogin_Authentica tion",
regitParams, ref regitDR);

while(regitDR.R ead())
{
compareENum = regitDR["emp_Number "].ToString();
compareEPwd = regitDR["emp_Passwo rd"].ToString();
}
regitDR.Close() ;
regitDB.regitCo nnectionClose() ;
regitDB.regitCo nnectionDispose ();

if(compareENum= =EmpNumber && compareEPwd==Pa ssword)
{
return true;
}
else
{
return false;
}
}
catch(Exception ex)
{
throw(ex);
return false;
}
finally
{
regitDR.Close() ;
regitDB.regitCo nnectionClose() ;
regitDB.regitCo nnectionDispose ();
regitParams = null;
}
}
public bool regitCreateLogi n(string EmpNumber, string Password)
{
regitDatabase regitDB = new regitDatabase() ;
SqlParameter[] regitParams = new SqlParameter[2];

try
{
regitParams[0] = regitDB.regitMa keParameter("@E mpNumber",
EmpNumber);
regitParams[1] = regitDB.regitMa keParameter("@E mpPassword",
Password);
regitDB.regitRu nProcedure("reg it_sp_Add_UserL ogin", regitParams);

return true;
}
catch(SqlExcept ion sqlex)
{
throw(sqlex);
}
catch
{return false;}
finally
{
regitParams = null;
regitDB.regitCo nnectionClose() ;
regitDB.regitCo nnectionDispose ();
}
}
}
}

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 InvalidOperatio nException("A user is already logged into the application: " + loginUser.UserN ame);

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("use r 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("nam e",
"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
GenericPrincipa l instance when the user first logs in. Then, assign the principal to the current Thread via the static
Thread.CurrentP rincipal property. It can be retrieved at anytime by any code that executes on that Thread. (see the
System.Security .Principal namespace and the System.Threadin g namespace).

--
Dave Sexton

"Matthew" <ma******@yahoo .comwrote in message news:11******** **************@ b28g2000cwb.goo glegroups.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

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

Similar topics

7
1632
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 crunching". (And Perl could beat Python, and Fortran could kick all their butts, etc...) Well, you'd think me and the other guy would be old enough to know better, or at least have more important things to do, but nooooo... Ah well, too late...
7
4383
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 few features that you'd expect in any editor, except nearly none of them seem to have: 1 - Search and repalce with Regular Expressions. 2 - Search and Replace in an Xpath context. 3 - User specified tag-generation for either on a...
9
2921
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 a device. The definition of the table is as follows: CREATE TABLE devicedata ( device_id int NOT NULL REFERENCES devices(id), -- id in the device
19
4087
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 the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can easily understand a newbie using bound controls or someone with a tight deadline. I guess I need...
5
4398
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 was quite insestual for while wasn't it? I need to write putchar so that printf can function properly. Anyway, the compiler comes with just a shell of a putchar routine. It literally returns the character you passed to it and nothing else. That is...
7
2356
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 WinForms interface and then need to port that to a web app. I am kinda stuck on a design issue and need some suggestions / direction. Basically I have a business layer that I want to use to process any dataentry logic (row focus changes, data...
18
3026
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
1251
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 on the site). I'm using a Class called ProductBoxes and inside I have a public property of Object type and I call from the PageLoad event dim boxes as new ProductBoxes
8
2736
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 the sequence nos and it should be such that i can access it through the structure .plz help me .
7
2244
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 { private: int number; public: Something ();
0
8372
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8285
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8706
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8475
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7304
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1592
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.