![](/img/trans.png)
[英]Session vs User.Identity.Name to check the loggeduser name for ASP.net core application with windows authentication
[英]ASP.NET web form application User.Identity.Name not working in IIS
我正在使用 ASP.NET Web 表單應用程序並嘗試在其中一個頁面中訪問 User.Identity.Name。
但是,它在 VS 中運行良好,但在 IIS 中返回空白。
很可能您在應用程序中使用了正確的代碼,但配置文件未針對 Windows 身份驗證進行設置。
確保這些部分在您的 web.config 文件中
<configuration>
<system.web>
<authentication mode="Windows" />
<authorization>
<deny users="?"/> <!-- Require everyone to log in -->
<!-- add specific allow deny for users and roles here -->
</authorization>
</system.web>
</configuration>
一旦這些元素到位,您就可以訪問登錄的用戶信息
// this will be in form Domain\Username
string UserIdentity = User.Identity.Name;
// or we can split and grab the second element
string[] DomainAndUser = User.Identity.Name.split('\\');
string UserDomain = DomainAndUser[0];
strung UserName = DomainAndUser[1];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.