MAC
MAC

Reputation: 6577

How to remove the domain name?

I have one sharepoint application, in this i have to show the current user, i used SPContext.Current.Web.CurrentUser.LoginName. then it returns XXXXXX\abida. But i want only the username like abida. How to achieve this requirement?

Upvotes: 1

Views: 4221

Answers (2)

Fenton
Fenton

Reputation: 251012

Note that we have to escape the slash...

string loginName = SPContext.Current.Web.CurrentUser.LoginName;
string[] loginNameParts = loginName.Split('\\');
string loginNameWithoutDomain = nameParts[1];

I presume you are doing this in order to use the name-only for some reason and that you aren't relying on the user name being unique in its own right. You could have DOMAIN1\BobSmith and DOMAIN2\BobSmith - so if you were using "BobSmith" as a unique user name, you could come unstuck.

Upvotes: 3

TomTom
TomTom

Reputation: 62109

you do not. The name is not guaranteed to be unique without the domain prefix. If you erally want to show it without, then just remove it - split the string at the "\" and use the second element. There are multiple ways do do that, from the Split method on the string to using IndexOf for the "\" and then substring to extract the reminder.

Upvotes: 0

Related Questions