Saturday, March 31, 2012

Trim to a specific alphanumeric quotable

HI all,

I am using c# and I am trying to use the windows authentication to recognize the users.
(User.Identity.Name)

Unfortunately, the users will belong to different domains and there fore their identity will look different. i.e. domain\username or net\username.

How can I remove everything before the username including the backslash?

thank you in advance,

GorginioHave you looked into substring??

// psuedo code

userName = User.Identity.Name.SubString(User.Identity.Name.IndexOf("\"));

That may or may not work.
Welcome to the ASP.NET forums, Gorginio.

You can use the following code:

 // get the username ... but I'll fake it here
string fullUsername = @."domain\username";

// trim away any domain that may form part of the username
string trimmedUsername;
int indexOfSlash = fullUsername.IndexOf( @."\" );
if (indexOfSlash > -1)
{
trimmedUsername = fullUsername.Substring( indexOfSlash + 1, fullUsername.Length - indexOfSlash - 1 );
}
else
{
trimmedUsername = fullUsername;
}

I hope this helps.

0 comments:

Post a Comment