Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 414
  • Last Modified:

Need to parse a string domain name and domain user.

Need to parse this string into two strings

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

="CWB\\mxcupryk"      

sDomain = ?
sUser = ?
0
mathieu_cupryk
Asked:
mathieu_cupryk
  • 4
  • 3
  • 3
1 Solution
 
Rob SiklosCommented:

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
 
naspinskiCommented:
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
 
naspinskiCommented:
better approach
string[] split = parseThis.Split(new string[] { "\\\\", "\"","=" }, StringSplitOptions.RemoveEmptyEntries);
string sDomain = split[0];
string sUser = split[1];

Open in new window

0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
mathieu_cuprykAuthor Commented:
sDomain  = CWB\mxcupryk?
sUser  bombs {"Index was outside the bounds of the array."}
0
 
mathieu_cuprykAuthor Commented:
nasp

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

should be CWB

and sUser = mxcupryk
0
 
Rob SiklosCommented:
string fullname = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
string[] parts = fullname.Split('\\');
 
string sDomain = parts[0];
string sUser parts[1];
0
 
naspinskiCommented:
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
 
naspinskiCommented:
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
 
Rob SiklosCommented:
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
 
mathieu_cuprykAuthor Commented:
great job rob

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now