How to get username without domain

前端 未结 7 648
一整个雨季
一整个雨季 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:32

    If you are using .NET 3.5 you could always create an extension method to the WindowsIdentity class that does this work for you.

    public static string NameWithoutDomain( this WindowsIdentity identity )
    {
        string[] parts = identity.Name.Split(new char[] { '\\' });
    
        //highly recommend checking parts array for validity here 
        //prior to dereferencing
    
        return parts[1];
    }
    

    that way all you have to do anywhere in your code is reference:

    Request.LogonUserIdentity.NameWithoutDomain();

提交回复
热议问题