简体   繁体   中英

Asp Net get username

I Googled about this, but none of solutions solved this situation.

My web.config:

<configuration>
    <system.web>
        <customErrors mode="Off"/>
        <compilation debug="true">
            </compilation>
      <authentication mode="Windows" />

    </system.web>
</configuration>

My aspx page:

<script runat="server">
    void Page_Load(object sender, EventArgs e)
    {
        string opl = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
        Response.Write("Name:" + opl);
    }
</script>

I'm getting an empty string, any ideas?I tried Request.ServerVariables but get an empty string for user also.

If you want to get the name of the logged user you can use

var user = User.Identity.Name

or

var user = User.Identity.UserName

尝试以下方法:

var username = User.Identity.Name

What you want to do is consult the HttpContext:

http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user.aspx

It will contain all of the information about the currently logged in user.

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