Solved

Need to parse a string domain name and domain user.

Posted on 2008-06-12
10
409 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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