从 C# 中的登录 ID 中删除域信息

发布于 2024-07-06 18:09:37 字数 514 浏览 5 评论 0原文

我想从 C# 中的登录 ID 中删除域/计算机信息。 所以,我想让“Domain\me”或“Domain\me”只是“我”。 我总是可以检查其中任何一个是否存在,并使用它作为索引来启动子字符串......但我正在寻找更优雅和紧凑的东西。

最坏的情况:

int startIndex = 0;
int indexOfSlashesSingle = ResourceLoginName.IndexOf("\");
int indexOfSlashesDouble = ResourceLoginName.IndexOf("\\");
if (indexOfSlashesSingle != -1)
    startIndex = indexOfSlashesSingle;
else
    startIndex = indexOfSlashesDouble;
string shortName = ResourceLoginName.Substring(startIndex, ResourceLoginName.Length-1);

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 check for the existence of either, and use that as the index to start the substring...but I am looking for something more elegant and compact.

Worse case scenario:

int startIndex = 0;
int indexOfSlashesSingle = ResourceLoginName.IndexOf("\");
int indexOfSlashesDouble = ResourceLoginName.IndexOf("\\");
if (indexOfSlashesSingle != -1)
    startIndex = indexOfSlashesSingle;
else
    startIndex = indexOfSlashesDouble;
string shortName = ResourceLoginName.Substring(startIndex, ResourceLoginName.Length-1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

蓦然回首 2024-07-13 18:09:37

当你只有一把锤子时,一切看起来都像钉子......

使用刀片----

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

    }

}

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

    }

}
弥繁 2024-07-13 18:09:37

您可以滥用 Path 类,因此:

string shortName = System.IO.Path.GetFileNameWithoutExtension(ResourceLoginName);

You could abuse the Path class, thusly:

string shortName = System.IO.Path.GetFileNameWithoutExtension(ResourceLoginName);
葬シ愛 2024-07-13 18:09:37

怎么样:

string shortName = ResourceLoginName.Split('\\')[1]

How's about:

string shortName = ResourceLoginName.Split('\\')[1]
千纸鹤 2024-07-13 18:09:37

这适用于两者,但适用于命名组。

^(?<domain>.*)\\(?<username>.*)|(?<username>[^\@]*)@(?<domain>.*)?$

This will work for both but with named groups.

^(?<domain>.*)\\(?<username>.*)|(?<username>[^\@]*)@(?<domain>.*)?$
各空 2024-07-13 18:09:37

我总是这样做:

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

现在你可以查看domainuser.Length来看看有多少部分,domainuser[0]表示域,domainuser[1]表示用户名。

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.

回忆追雨的时光 2024-07-13 18:09:37

这适用于两个有效的域登录:

var regex = @"^(.*\\)?([^\@]*)(@.*)?$";
var user = Regex.Replace("domain\\user", regex, "$2", RegexOptions.None);
user = Regex.Replace("[email protected]", regex, "$2", RegexOptions.None);

This works for both valid domain logins:

var regex = @"^(.*\\)?([^\@]*)(@.*)?$";
var user = Regex.Replace("domain\\user", regex, "$2", RegexOptions.None);
user = Regex.Replace("[email protected]", regex, "$2", RegexOptions.None);
烟酉 2024-07-13 18:09:37
        string theString = "domain\\me";
        theString = theString.Split(new char[] { '\\' })[theString.Split(new char[] { '\\' }).Length - 1];
        string theString = "domain\\me";
        theString = theString.Split(new char[] { '\\' })[theString.Split(new char[] { '\\' }).Length - 1];
清晨说晚安 2024-07-13 18:09:37

小猪支持德里克·斯莫斯的回答……

Regex.Replace(User.Identity.Name,@"^(?<domain>.*)\\(?<username>.*)|(?<username>[^\@]*)@(?<domain>.*)?$", "${username}", RegexOptions.None)

对我有用。

Piggy backing on Derek Smalls Answer...

Regex.Replace(User.Identity.Name,@"^(?<domain>.*)\\(?<username>.*)|(?<username>[^\@]*)@(?<domain>.*)?$", "${username}", RegexOptions.None)

worked for me.

复古式 2024-07-13 18:09:37

作为 @Drew 答案的改进

User.Identity.Name.Split('\\').Last()

,这样您还可以匹配带域和不带域的用户名

As an improvement of @Drew's answer

User.Identity.Name.Split('\\').Last()

This way you can also match usernames with and without domain

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文