Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need to parse a string domain name and domain user.

Posted on 2008-06-12
10
Medium Priority
?
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 

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 2000 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 learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

721 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