How to get username without domain

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

    Getting parts[1] is not a safe approach. I would prefer use LINQ .Last():

    WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
    if (windowsIdentity == null)
        throw new InvalidOperationException("WindowsIdentity is null");
    string nameWithoutDomain = windowsIdentity.Name.Split('\\').Last();
    

提交回复
热议问题