下午全部,
我有一个网站,我需要知道谁将记录插入我的数据库表。我有一组用户,我已经填充到Active Directory(AD)组,反过来他们是唯一能够访问该站点的成员。 (请参阅下面的web.config代码和.vb代码)
'配置文件
<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代码
If User.IsInRole(ConfigurationManager.AppSettings("Users")) Then
Else
Response.Redirect("NotAuthorised.aspx")
End If
我有一个存储过程,用于将表单字段中的数据插入数据库,工作正常。
我遇到的问题是我没有要求用户登录,因为他们可以通过AD设置这样做。但是我需要设置用户名,以便我可以将其填充到我的数据库表中,以说明谁添加了该记录。
我可以用一些VB代码来获取这些信息吗?
非常感谢提前 贝蒂。
答案 0 :(得分:5)
HttpContext.Current.User.Identity.Name
根据OP的代码User.Identity.Name
,仅User
在范围内
会给你用户名。 use可以使用
访问其他信息 User.Identity
答案 1 :(得分:3)
您可以使用您的应用程序获取当前用户名:
string loggedUser = HttpContext.Current.User.Identity.Name.ToString();
完成后,您可以存储在数据库中
将loggedUser传递给数据库
答案 2 :(得分:0)
正如其他人所说,您可以使用User.Identity.Name
获取已登录用户的名称。
如果在web.config中添加适当的标记,还可以删除检查角色成员身份的代码。只有在事先不知道角色名称且无法存储在web.config文件中时,检查代码中的成员资格才有用。当角色来自数据库时。
您可以specify the roles授权使用location标记访问特定网页:
<configuration>
....
<location path="MySecurePage.aspx">
<system.web>
<authorization>
<allow roles="Administrators,MyGroup" />
<deny users="*"/>
</authorization>
</system.web>
</location>
</configuration>