Built-in helper to parse User.Identity.Name into Domain\Username

后端 未结 8 642
面向向阳花
面向向阳花 2020-12-02 22:23

Is there any built-in utility or helper to parse HttpContext.Current.User.Identity.Name, e.g. domain\\user to get separately domain name if exists

相关标签:
8条回答
  • 2020-12-02 23:00

    Seems like a problem made to be solved by regular expressions:

    public static class UserExtensions
    {
        public static string GetDomain(this IIdentity identity)
        {
            Regex.Match(identity.Name, ".*\\\\").ToString()
        }
    
        public static string GetLogin(this IIdentity identity)
        {
            return Regex.Replace(identity.Name, ".*\\\\", "");
        }
    }
    
    0 讨论(0)
  • 2020-12-02 23:05

    System.Environment.UserDomainName gives you the domain name only

    Similarly, System.Environment.UserName gives you the user name only

    0 讨论(0)
提交回复
热议问题