我正在研究在Intranet上运行的ASP.NET MVC应用程序。我们正在使用表单身份验证,但是设置的用户名将与Active Directory用户名相同。我正在尝试找到一种使用Windows用户名预填充登录用户名字段的方法,但是找不到访问当前Windows用户身份的方法。System.Environment.UserDomainName
返回“ IIS APPPOOL”System.Security.Principal.WindowsPrincipal.Current.Identity
返回GenericIdentityHttpContext.Request.LogonUserIdentity.Name
返回“ NT AUTHORITY \ IUSR”
我不想认证Windows用户或以Windows用户身份登录网站,我只想读取用户名。使用窗体身份验证而不是Windows身份验证时,有什么方法可以做到这一点?
最佳答案
请尝试以下方法:
在web.config中
<identity impersonate="true" />
<authentication mode="Forms">
<forms loginUrl="./WebLogin.aspx" defaultUrl="./default.aspx" timeout="12" path="/" name=".ASPXFORMSAUTH" />
</authentication>
在代码C#中
string windowsName = WindowsPrincipal.Current.Identity.Name;
//OR
string windowsName = Thread.CurrentPrincipal.Identity.Name;
这对我来说很好。