Solved

Need to parse a string domain name and domain user.

Posted on 2008-06-12
10
407 Views
Last Modified: 2013-12-17
Need to parse this string into two strings

System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString()      

="CWB\\mxcupryk"      

sDomain = ?
sUser = ?
0
Comment
Question by:mathieu_cupryk
  • 4
  • 3
  • 3
10 Comments
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21770090

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

string[] parts = fullname.Split(new string[] {@"\\"}, StringSplitOptions.None);
 

string sDomain = parts[0];

string sUser parts[1];

Open in new window

0
 
LVL 21

Expert Comment

by:naspinski
ID: 21770143
this will work (notice that I have to use double \\ for each one as '\' is the escape character in c#.

 I just thought of a cleaner way too, will post it in a second
string parseThis = "=\"CWB\\\\mxcupryk\"";

string[] split = parseThis.Substring(3, parseThis.Length - 3).Substring(0, parseThis.Length - 4).Split(new string[] { "\\\\" }, StringSplitOptions.RemoveEmptyEntries);

string sDomain = split[0];

string sUser = split[1];

Open in new window

0
 
LVL 21

Expert Comment

by:naspinski
ID: 21770166
better approach
string[] split = parseThis.Split(new string[] { "\\\\", "\"","=" }, StringSplitOptions.RemoveEmptyEntries);

string sDomain = split[0];

string sUser = split[1];

Open in new window

0
 

Author Comment

by:mathieu_cupryk
ID: 21770179
sDomain  = CWB\mxcupryk?
sUser  bombs {"Index was outside the bounds of the array."}
0
 

Author Comment

by:mathieu_cupryk
ID: 21770222
nasp

sDomain = "CWB\\mxcupryk"
sUser  bombs {"Index was outside the bounds of the array."}

should be CWB

and sUser = mxcupryk
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21770237
string fullname = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
string[] parts = fullname.Split('\\');
 
string sDomain = parts[0];
string sUser parts[1];
0
 
LVL 21

Expert Comment

by:naspinski
ID: 21770260
If I run the following, I get this exact output:

parsing: ="CWB\\mxcupryk"
sDomain=CWB
sUser=mxcupryk
string parseThis = "=\"CWB\\\\mxcupryk\"";

string[] split = parseThis.Split(new string[] { "\\\\", "\"","=" }, StringSplitOptions.RemoveEmptyEntries);

string sDomain = split[0];

string sUser = split[1];

//added for testing

Response.Write("parsing: " + parseThis + "<br />sDomain=" + sDomain + "<br />sUser=" + sUser);

Open in new window

0
 
LVL 21

Expert Comment

by:naspinski
ID: 21770290
or possibly change to this as the double / wont make a difference
string[] split = parseThis.Split(new string[] { "\\", "\"","=" }, StringSplitOptions.RemoveEmptyEntries);

string sDomain = split[0];

string sUser = split[1];

Open in new window

0
 
LVL 9

Accepted Solution

by:
Rob Siklos earned 500 total points
ID: 21770345
it looks like the value of the full name is CWB\mxcupryk (no =, no quotes).

In such a case, use this:
string fullname = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

string[] parts = fullname.Split('\\');

 

string sDomain = parts[0];

string sUser parts[1];

Open in new window

0
 

Author Comment

by:mathieu_cupryk
ID: 21770401
great job rob

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

943 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now