48

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 and user?

Or is there any other class to do so?

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

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 1
    It's the simple questions we always forget to ask ourselves. Will look forward to any useful answers to this question. – Torbjørn Dec 08 '08 at 13:25

8 Answers8

70

This is better (easier to use, no opportunity of NullReferenceExcpetion and conforms MS coding guidelines about treating empty and null string equally):

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;
    }
}

Usage:

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

This requires C# 3.0 compiler (or newer) and doesn't require 3.0 .Net for working after compilation.

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
Aen Sidhe
  • 1,181
  • 12
  • 25
  • 8
    Your return statement in `GetLogin` can be simplified to `return s.Substring(stop + 1);` – Sam Harwell Aug 01 '13 at 12:31
  • 1
    What if the username has multiple back-slashes? What if the user wants to use the UPN format of user@domain.com? I ended up using the Win32 function CredUIParseUserName. See http://www.pinvoke.net/default.aspx/credui.creduiparseusername – Ed Greaves Oct 04 '17 at 15:57
13

System.Environment.UserDomainName gives you the domain name only

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

StarCub
  • 4,141
  • 7
  • 41
  • 58
  • 2
    > System.Environment.UserDomainName` gives you the domain name only > > Similarly, `System.Environment.UserName` gives you the user name only This will not work on ASP.NET – Simon Bastian Aug 23 '11 at 08:03
  • I think it works if you use authentication=Windows and impersonation=true. See - http://stackoverflow.com/questions/8841816/system-web-httpcontext-current-user-identity-name-vs-system-environment-username – FMFF Jan 12 '12 at 21:40
7
var components = User.Identity.Name.Split('\\');

var userName = components.Last() 

var domainName = components.Reverse().Skip(1).FirstOrDefault()
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Gruff Bunny
  • 27,738
  • 10
  • 72
  • 59
5

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

This is what I'm currently doing:
If string contains '\' then split string at '\' and extract username and domain
Else If string contains '@' then split string at '@' and extract username and domain
Else treat string as username without a domain

I'm still hunting for a better solution in the case where the input string isn't in an easily predicted format, i.e. "domain\user@domain". I'm thinking RegEx...

Update: I stand corrected. My answer is a bit of out context, it refers to the general case of parsing username and domains out of user input, like in user login/logon prompt. Hope it still helps someone.

Omar
  • 1,493
  • 12
  • 14
  • Need an elegant solution for this. I agree. One answer to the domain\user part is this one: http://stackoverflow.com/a/185716/481656 – DoomerDGR8 Jun 06 '12 at 08:51
4

I think No too, 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;
}
Unihedron
  • 10,902
  • 13
  • 62
  • 72
Ian G
  • 29,468
  • 21
  • 78
  • 92
1

Although not a .NET built-in, one can always P/Invoke to CredUIParseUserName. Here's a example of how to use it in .NET.

PS: It doesn't seem to handle the "dot", as in ".\username".

Mihai
  • 53
  • 4
1

I don't think so, because System.Security.Principal.WindowsIdentity doesn't contain such members.

Aen Sidhe
  • 1,181
  • 12
  • 25
0

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, ".*\\\\", "");
    }
}
Adam Cooper
  • 8,077
  • 2
  • 33
  • 51