Built-in helper for analyzing User.Identity.Name in Domain \ Username

Is there a built-in utility or an assistant for parsing HttpContext.Current.User.Identity.Name , for example. domain\user to get a separate domain name, if exists, and user?

Or is there another class?

I understand that it is very easy to call it String.Split("\") , but just interesting

+46
authentication c # active-directory
Dec 08 '08 at 13:14
source share
8 answers

This is better (easier to use, without the possibility of NullReferenceExcpetion and complies with MS encoding rules regarding uniform input of empty and zero lines):

 public static class Extensions { public static string GetDomain(this IIdentity identity) { string s = identity.Name; int stop = s.IndexOf("\\"); return (stop > -1) ? s.Substring(0, stop) : string.Empty; } public static string GetLogin(this IIdentity identity) { string s = identity.Name; int stop = s.IndexOf("\\"); return (stop > -1) ? s.Substring(stop + 1, s.Length - stop - 1) : string.Empty; } } 

Using:

 IIdentity id = HttpContext.Current.User.Identity; id.GetLogin(); id.GetDomain(); 

This requires a C # 3.0 compiler (or newer) and does not require 3.0.Net to work after compilation.

+68
Dec 08 '08 at 16:53
source share

System.Environment.UserDomainName provides only the domain name

Similarly, System.Environment.UserName gives you only the username

+13
Jun 08 2018-11-21T00:
source share
 var components = User.Identity.Name.Split('\\'); var userName = components.Last() var domainName = components.Reverse().Skip(1).FirstOrDefault() 
+5
Feb 27 '13 at 16:30
source share

I think not, because I asked myself the same question the other day: D

You can try:

 public static string GetDomain(string s) { int stop = s.IndexOf("\\"); return (stop > -1) ? s.Substring(0, stop + 1) : null; } public static string GetLogin(string s) { int stop = s.IndexOf("\\"); return (stop > -1) ? s.Substring(stop + 1, s.Length - stop - 1) : null; } 
+4
Dec 08 '08 at 14:09
source share

You guys can also consider parsing a string, like user@company.com, or "user @domain".

This is what I am doing now:
If the string contains '\', divide the string by '\' and extract the username and domain
Else If the string contains "@", divide the string by "@" and extract the username and domain
Else treat string as username without domain

I'm still looking for a better solution when the input string is not in an easily predicted format, that is, "domain \ user @domain". I think RegEx ...

Update: I am standing fixed. My answer is a bit out of context, it refers to the general case of analyzing a username and domains outside of user input, for example, at a user / login prompt. Hope this still helps someone.

+3
Feb 01 '10 at
source share

I don’t think so, because System.Security.Principal.WindowsIdentity does not contain such members.

+1
Dec 08 '08 at 13:27
source share

Although this is not native .NET, you can always use P / Invoke CredUIParseUserName . Here is an example of use in .NET.

PS: It does not seem to handle the "dot" as in ". \ Username".

+1
Apr 03 '14 at 11:16
source share

It seems that the problem has been resolved with 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
Aug 01 '13 at 12:14
source share



All Articles