Reputation: 185
Afternoon All,
I have a web site where i need to know who is inserting a record into my database table. I have a group of users which i have populated into an Active Directory (AD) group and in turn they are the only members able to access the site. (See web.config code and .vb code below)
'Config File
<appSettings>
<add key="dbConnection" value="Data Source=xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx;"/>
<add key="Users" value="Users_General_Access"/>
</appSettings>
'Vb Code
If User.IsInRole(ConfigurationManager.AppSettings("Users")) Then
Else
Response.Redirect("NotAuthorised.aspx")
End If
I have a stored procedure that is used to insert data from my form fields into the database which work fine.
The problem that i have is that im not asking the user to log in as they are able to do so via the AD set up. However i need to caputre the username so i can populate this into my database table to state who has added that record.
Is there some VB code that i can use to obtain this information?
Many thanks in advance Betty.
Upvotes: 1
Views: 3243
Reputation: 131227
As other have said, you can get the name of the logged-in user with User.Identity.Name
.
You can also get rid of the code that checks for role membership if you add the proper tags in web.config. Checking membership in code is useful only if the name of the role is not known in advance and can't be stored in the web.config file, eg. when the roles come from a database.
You can specify the roles authorized to visit a specific page using the location tag:
<configuration>
....
<location path="MySecurePage.aspx">
<system.web>
<authorization>
<allow roles="Administrators,MyGroup" />
<deny users="*"/>
</authorization>
</system.web>
</location>
</configuration>
Upvotes: 0
Reputation: 35572
HttpContext.Current.User.Identity.Name
OR only User.Identity.Name
as according to OP's code User
is in the scope
will give you user name. use can access other information using
User.Identity
Upvotes: 6
Reputation: 11916
You can get the Current User Name using your Application by:
string loggedUser = HttpContext.Current.User.Identity.Name.ToString();
Once you have this, you can store in Database
Pass the loggedUser to the database
Upvotes: 3