views:

1551

answers:

4

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

Or is there any other class to do so?

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

+1  A: 

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

Aen Sidhe
+2  A: 

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;
}
DrG
+5  A: 

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

public static string GetDomain(this IIdentity identity)
{
    string s = identity.Name;
    int stop = s.IndexOf("\\");
    return (stop > -1) ?  s.Substring(0, stop + 1) : 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.

Aen Sidhe
+1  A: 

You guys might also consider parsing a string input like "[email protected]", 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