Remove domain information from login id in C#

后端 未结 8 2432
谎友^
谎友^ 2021-02-12 12:02

I would like to remove the domain/computer information from a login id in C#. So, I would like to make either \"Domain\\me\" or \"Domain\\me\" just \"me\". I could always chec

相关标签:
8条回答
  • 2021-02-12 12:22

    I always do it this way:

        string[] domainuser;
        string Auth_User = Request.ServerVariables["AUTH_USER"].ToString().ToLower(); 
        domainuser = Auth_User.Split('\\');
    

    Now you can look at domainuser.Length to see how many parts are there and domainuser[0] for the domain and domainuser[1] for the username.

    0 讨论(0)
  • 2021-02-12 12:23

    You could abuse the Path class, thusly:

    string shortName = System.IO.Path.GetFileNameWithoutExtension(ResourceLoginName);
    
    0 讨论(0)
  • 2021-02-12 12:26

    when all you have is a hammer, everything looks like a nail.....

    use a razor blade ----

    using System;
    using System.Text.RegularExpressions;
    public class MyClass
    {
        public static void Main()
        {
            string domainUser = Regex.Replace("domain\\user",".*\\\\(.*)", "$1",RegexOptions.None);
            Console.WriteLine(domainUser);  
    
        }
    
    }
    
    0 讨论(0)
  • 2021-02-12 12:33
            string theString = "domain\\me";
            theString = theString.Split(new char[] { '\\' })[theString.Split(new char[] { '\\' }).Length - 1];
    
    0 讨论(0)
  • 2021-02-12 12:34

    How's about:

    string shortName = ResourceLoginName.Split('\\')[1]
    
    0 讨论(0)
  • 2021-02-12 12:34

    This will work for both but with named groups.

    ^(?<domain>.*)\\(?<username>.*)|(?<username>[^\@]*)@(?<domain>.*)?$
    
    0 讨论(0)
提交回复
热议问题