How to get username without domain

前端 未结 7 647
一整个雨季
一整个雨季 2021-02-06 21:12

In an aspx page I get the Windows username with the function Request.LogonUserIdentity.Name. This function returns a string in the format \"domain\\user\".

7条回答
  •  孤街浪徒
    2021-02-06 21:42

    I don't believe so. I have got the username using these methods before-

    var user = System.Web.HttpContext.Current.User;   
    var name = user.Identity.Name;
    
    var slashIndex = name.IndexOf("\\");
    return slashIndex > -1 
        ? name.Substring(slashIndex  + 1)
        : name.Substring(0, name.IndexOf("@"));
    

    or

    var name = Request.LogonUserIdentity.Name;
    
    var slashIndex = name.IndexOf("\\");
    return slashIndex > -1 
        ? name.Substring(slashIndex  + 1)
        : name.Substring(0, name.IndexOf("@"));
    

提交回复
热议问题