Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get username without domain

Tags:

asp.net

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".

Is there some function to only get the username, without resorting to the IndexOf and Substring, like this?

public static string StripDomain(string username) {     int pos = username.IndexOf('\\');     return pos != -1 ? username.Substring(pos + 1) : username; } 
like image 656
doekman Avatar asked Dec 01 '08 09:12

doekman


People also ask

What is a domain username?

A domain user is one whose username and password are stored on a domain controller rather than the computer the user is logging into. When you log in as a domain user, the computer asks the domain controller what privileges are assigned to you.


2 Answers

If you are using Windows Authentication. This can simply be achieved by calling System.Environment.UserName which will give you the user name only. If you want only the Domain name you can use System.Environment.UserDomainName

like image 175
Robin V. Avatar answered Sep 28 '22 17:09

Robin V.


I don't believe so. I have got the username using these methods before-

var user = System.Web.HttpContext.Current.User;    var name = user.Identity.Name;  var slashIndex = name.IndexOf("\\"); return slashIndex > -1      ? name.Substring(slashIndex  + 1)     : name.Substring(0, name.IndexOf("@")); 

or

var name = Request.LogonUserIdentity.Name;  var slashIndex = name.IndexOf("\\"); return slashIndex > -1      ? name.Substring(slashIndex  + 1)     : name.Substring(0, name.IndexOf("@")); 
like image 34
Russ Cam Avatar answered Sep 28 '22 16:09

Russ Cam