Removing or Trimming \ (back slash)
-
Wednesday, January 09, 2008 6:41 PM
I need some help please!
I am writing an ASP.Net web app and I am trying to get the Current User Identity and then bounce that off a database to see if the user is authorized to use the app.
I get the identity fine (by using CurrentIdentity.Name) but I get back the computer name and the user name like “computername\username”.
How can I remove or trim off the “computername\” to get just the user name?
I have seen a lot of examples for getting the file name and trimming with Char [] but when I use the \ I get errors when I build.
Thanks for any and all help!
Kurt
All Replies
-
Wednesday, January 09, 2008 7:28 PM
The String class has numerous methods that you might find useful. If you know the computername ahead of time, then you know how many characters are on the front of the string.
Code Blockvoid
MethodA(){
string user = "computername";
user = user.Remove(IntStartIndex, IntCharCount);
}
IntStartIndex would be here.....at the red slash...well just past it.
Code Blockcomputername\usernameIntCharCount is how many characters to the end of the string. That would be the difference between the overall length of "user" and the length of the computername on the front. Hope this helps.
Rudedog
- Proposed As Answer by Dreamdao Saturday, December 26, 2009 12:12 PM
-
Wednesday, January 09, 2008 7:34 PMBackslashes begin escape sequences. You might want to escape the backslash with another backslash, e.g.:
string a = "\\"; -> \
Hope this helps. -
Wednesday, January 09, 2008 8:02 PMWhat about:
String userName = userName.Substring(userName.IndexOf('\') + 1); -
Thursday, January 10, 2008 11:00 AMHow about Path.GetFileName()?
-
Thursday, January 10, 2008 1:42 PM
Thanks to everyone who has replied, I appreciate the help!
So far I have not had any luck with the suggestions.
Matt, on the Path.GetFileName option I only have PathDirection available. Am I missing a using statement or something?
I am thinking I need to parse this data out and then select what I need. Does that make sense to anyone? If so then I would need to figure that out, it is just a thought I had.
Sorry I am relatively new to programming.
Thanks,
Kurt
-
Thursday, January 10, 2008 3:24 PM
try
stringObject.Substring(stringObject.IndexOf("\\")+1)
Also read about Substring @ http://msdn2.microsoft.com/en-us/library/hxthx5h6.aspx
Cheers
-
Thursday, January 10, 2008 3:39 PM
C# DevMan wrote: Matt, on the Path.GetFileName option I only have PathDirection available. Am I missing a using statement or something?
I think the full thing is System.IO.Path.GetFileName() -
Thursday, January 10, 2008 8:41 PM
Like Matt said
System.IO.Path.GetFileName(@"computername\username")
works.
I'm pretty suprised as I neved tought of using this function for something else than a filename. Clever
-
Thursday, January 10, 2008 9:06 PM
System.IO.Path.GetFileName is a terrible idea. It uses the System.IO.Path.DirectorySeparatorChar as the separator char, and this is NOT guaranteed to be \ - for example on Japanese systems it is the Yen symbol, and if the application were to run on Mono then it would be a / on Unix/Linux. Don't use functions for things they aren't designed for, especially if you don't understand the limitations.
A safe way to do it, which would handle the possibility that the name is returned without the domain, which is possible for built-in accounts, would be:
int separatorIndex = myUsernameString.LastIndexOf(@"\");
if (separatorIndex != -1 && separatorIndex < myUsernameString.Length - 1)
{
myUsernameString = myUsernameString.Substring(separatorIndex + 1);
} -
Thursday, January 10, 2008 9:43 PM
Greg is right, forget what I just said.
-
Thursday, January 10, 2008 11:29 PM
Thanks again everyone for the suggestions!!
Greg,
I am trying to implement your suggestion but I am getting errors when I build. FYI: This will be running on a local Intranet only.
Can you help me with the code?
Here is the code I have which is working to grab the Current Identity and display the results on a label "computername\username".
using
System.Security.Principal;public
partial class TrimTest : System.Web.UI.Page{
protected void Page_Load(object sender, EventArgs e)
{
//grab the current user
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
//assigning the ID to the label to show results
LblUserID.Text = currentIdentity.Name;
}
}
Thanks,
Kurt
-
Friday, January 11, 2008 11:47 AM
Greg Beech wrote: System.IO.Path.GetFileName is a terrible idea. It uses the System.IO.Path.DirectorySeparatorChar as the separator char, and this is NOT guaranteed to be \ - for example on Japanese systems it is the Yen symbol, and if the application were to run on Mono then it would be a / on Unix/Linux.
This is true - but I was answering according to the title of this thread, which is "Removing or Trimming \ (back slash) From File Path"
-
Friday, January 11, 2008 4:25 PMAh good point - I hadn't spotted that the title didn't match the question!
In which case you're quite right, for file paths what you suggested is exactly the way to do it. -
Friday, January 11, 2008 6:33 PM
I'm confused am I missing something?
Did I not post the title and the question correctly?
I believe what was returned by the get current identity and that which I am trying to trim is a file path, correct?
Kurt
-
Friday, January 11, 2008 7:28 PM
C# DevMan wrote: I'm confused am I missing something?
Did I not post the title and the question correctly?
I believe what was returned by the get current identity and that which I am trying to trim is a file path, correct?
Kurt
Have you found a workable solution? Food for thought. It's easy to enumerate Arrays and ArrayList types.
Code Blockpublic
static string MyConvert(string s){
byte[] MyString = Convert.FromBase64String(s); string output = Convert.ToBase64String(MyString); return output;}
Rudedog
| C# DevMan wrote: | |
|
No it isn't a file path, it's the name of the identity.
Assuming that the current identity is a WindowsIdentity then you will get a string in the format DOMAIN\USERNAME where the DOMAIN\ part is theoretically optional for built-in Windows accounts such as LocalSystem. The major difference between this and a file path is that the separator character, if present, is guaranteed to be \ for the name of a windows identity, whereas the separator character in a file path is not guaranteed and is environment-specific, though it is usually \ which is what gives people the wrong impression that it is a constant.
The code I posted in my first post solves the issue.
-----
PS: Why on earth is the above post marked as the answer? I fail to see what converting a string to/from a base 64 string or a byte array even has to do with the original question.
Greg,
Thanks for the explanation / clarification.
I am having trouble implementing your code I replied to your post asking for help with it but did not hear back from you.
Please see that post which contains the code I have that is working up to where I am having the problem. Not sure if I am missing a reference or what on your solution but I get the error that the "LastIndexOf" does not exist...
I do not see that there has been an answer marked yet? This is still unanswered. I am sure Rudedog was just trying to be helpful which I appreciate.
Thanks for the help!
Kurt
Here is Greg's answer put in a function for easy use:
public
string GetUserName(){
string identity; int separator_index;
identity = System.Security.Principal.
WindowsIdentity.GetCurrent().Name;separator_index = identity.LastIndexOf(
@"\");if(separator_index != -1 && separator_index < identity.Length - 1)
identity = identity.Substring(separator_index + 1);
return identity;
}
Thanks lotUs!!
I will give that a shot.
Thanks again,
Kurt
That worked perfect!!! Thanks lOtUs!! and of course to Greg for the initial post!!
I need to read up on what it is actually doing (mostly the last index and the @"\") so I understand it more but it is giving me the correct results.
I am going to try to rename the topic so it is more accurate so hopefully others can benefit from it too! I am kind of new to forums so not sure if I can or not.
Thanks again,
Kurt
PS Thanks to everyone who offered suggestions!!

