就像标题所说的那样。需要我们的 asp.net 页面中的 Windows 登录和域信息。
我试过了
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
但它返回 IIS 应用程序池而不是用户名
谢谢
就像标题所说的那样。需要我们的 asp.net 页面中的 Windows 登录和域信息。
我试过了
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
但它返回 IIS 应用程序池而不是用户名
谢谢
Try HttpContext.User
,可以User
从后面的代码中轻松访问。它返回域和用户名,但应该很容易根据您的需要进行修剪。过去它对我有用。如果需要,您还可以使用它来管理应用程序中的角色。
编辑
下面是我的 web.config 的相关部分。我还使用aspnet_regsql.exe在我的数据库中设置角色管理器所需的表。然后我可以使用User.Identity.Name
和User.Identity.IsInRole
<connectionStrings>
<clear/>
<add name="SqlRoleManagerConnection"
connectionString="myConnectionString">
</add>
</connectionStrings>
<system.web>
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
<roleManager enabled="true" defaultProvider="SqlRoleManager">
<providers>
<clear/>
<add name="SqlRoleManager"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="SqlRoleManagerConnection"
applicationName="myAppName" />
</providers>
</roleManager>
</system.web>
这是我在 VB.net 中的代码
var strUser = System.Web.HttpContext.Current.User.Identity.Name
所以 C# 必须在以下行中:(未测试) string strUser = System.Web.HttpContext.Current.User.Identity.Name;
在 Web.Config 文件中
<configuration>
<system.web>
<authentication mode="Windows"/>
<identity impersonate="true" />
</system.web>
</configuration>