简体   繁体   中英

ASP.NET web form application User.Identity.Name not working in IIS

I am using ASP.NET web form application and trying to access User.Identity.Name in one of the page.

However, it is working fine in VS but returns blank in IIS.

Most likely you are using the correct code within your application, but the configuration file is not set up for Windows Authentication.

Make sure these portions are within your web.config file

<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>

Once these elements are in place, you can access the logged in user information

// 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];

MS: how to implement Windows Authentication

Scott Gu Blog at ASP.NET

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM