Logged in User without domain

M

Mrinal

Hi ,

I am dealing with a strange issue , that , i initially thought would be a sitter to implement , let me know if you have some
clue to resolve the issue :

In one of my business logic , i fetch the current logged on user as follows :

string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name ;

it fetches the username in the format : domain\username for e.g : st-users\mrinal

now , for my logic , i only need logged in user name viz : mrinal , now when i try removing the domain name portion with \
i.e i want to remove st-users\ , then neither regex nor char array has helped , as it's an escape sequence , so can't be
matched with a regex pattern . For converting to the char array it combines \ with next character when stored in an array
for e.g : here it stores \m , so , i can't get only username , that i need .

Any suggestions to implement this or may be easier option is if there's an API that can just give me logged in
username without domain name and \ .

Any help in this regard would be great .

thanks ,

Mrinal .
 
M

Morten Wennevik

Hi Mrinal,

Why not just use String.SubString(String.IndexOf("\\"))

I haven't used Regex much, but I would be surprised if it didn't a way to
escape the escape sequence, like in strings "\\" = \ and @"\" = \
 
M

Mrinal

Thanks Morten ,

That worked out for me , essentially i looked into all complex stuff , even when a simpler solution was right there .

However it should be :

String.SubString(String.IndexOf("\\") + 1) , as i want to start after \ .

Regex solution is surely possible , it's just that , i am not able to get my brain working on that .

Mrinal

Morten said:
Hi Mrinal,

Why not just use String.SubString(String.IndexOf("\\"))

I haven't used Regex much, but I would be surprised if it didn't a way
to escape the escape sequence, like in strings "\\" = \ and @"\" = \



Hi ,

I am dealing with a strange issue , that , i initially thought would
be a sitter to implement , let me know if you have some
clue to resolve the issue :

In one of my business logic , i fetch the current logged on user as
follows :

string username =
System.Security.Principal.WindowsIdentity.GetCurrent().Name ;

it fetches the username in the format : domain\username for e.g :
st-users\mrinal

now , for my logic , i only need logged in user name viz : mrinal ,
now when i try removing the domain name portion with \
i.e i want to remove st-users\ , then neither regex nor char array has
helped , as it's an escape sequence , so can't be
matched with a regex pattern . For converting to the char array it
combines \ with next character when stored in an array
for e.g : here it stores \m , so , i can't get only username , that i
need .

Any suggestions to implement this or may be easier option is if
there's an API that can just give me logged in
username without domain name and \ .

Any help in this regard would be great .

thanks ,

Mrinal .



--Happy Coding!
Morten Wennevik [C# MVP]
 
J

John B

Mrinal said:
Thanks Morten ,

That worked out for me , essentially i looked into all complex stuff ,
even when a simpler solution was right there .

However it should be :

String.SubString(String.IndexOf("\\") + 1) , as i want to start after \ .

Regex solution is surely possible , it's just that , i am not able to
get my brain working on that .
<...>

A regex that will work is .*\\(?<username> .*) (captures the username
part into named group username)
However, Morten's solution is surely simpler and more readable (probably
more performant too).

:)

JB
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top