C# 将 User.Identity.Name 解析为 Domain\Username 的内置助手

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/349520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 00:05:37  来源:igfitidea点击:

Built-in helper to parse User.Identity.Name into Domain\Username

c#asp.net.netauthenticationactive-directory

提问by abatishchev

Is there any built-in utility or helper to parse HttpContext.Current.User.Identity.Name, e.g. domain\userto get separately domain name if exists and user?

是否有任何内置实用程序或助手可以解析HttpContext.Current.User.Identity.Name,例如domain\user,如果存在和用户,则分别获取域名?

Or is there any other class to do so?

或者有没有其他课程可以这样做?

I understand that it's very easy to call String.Split("\")but just interesting

我知道这很容易打电话String.Split("\")但很有趣

采纳答案by Aen Sidhe

This is better (easier to use, no opportunity of NullReferenceExcpetionand conforms MS coding guidelines about treating empty and null string equally):

这更好(更容易使用,没有机会NullReferenceExcpetion并符合关于同等对待空字符串和空字符串的 MS 编码指南):

public static class Extensions
{
    public static string GetDomain(this IIdentity identity)
    {
        string s = identity.Name;
        int stop = s.IndexOf("\");
        return (stop > -1) ?  s.Substring(0, stop) : string.Empty;
    }

    public static string GetLogin(this IIdentity identity)
    {
        string s = identity.Name;
        int stop = s.IndexOf("\");
        return (stop > -1) ? s.Substring(stop + 1, s.Length - stop - 1) : string.Empty;
    }
}

Usage:

用法:

IIdentity id = HttpContext.Current.User.Identity;
id.GetLogin();
id.GetDomain();

This requires C# 3.0 compiler (or newer) and doesn't require 3.0 .Net for working after compilation.

这需要 C# 3.0 编译器(或更新版本)并且不需要 3.0 .Net 编译后工作。

回答by Aen Sidhe

I don't think so, because System.Security.Principal.WindowsIdentity doesn't contain such members.

我不这么认为,因为 System.Security.Principal.WindowsIdentity 不包含这样的成员。

回答by inspite

I think No too, because I asked myself the same question the other day :D

我也认为不,因为前几天我问了自己同样的问题:D

You can try:

你可以试试:

public static string GetDomain(string s)
{
    int stop = s.IndexOf("\");
    return (stop > -1) ? s.Substring(0, stop + 1) : null;
}

public static string GetLogin(string s)
{
    int stop = s.IndexOf("\");
    return (stop > -1) ? s.Substring(stop + 1, s.Length - stop - 1) : null;
}

回答by Omar

You guys might also consider parsing a string input like "[email protected]", or "user@domain".

你们也可以考虑解析像“[email protected]”或“user@domain”这样的字符串输入。

This is what I'm currently doing:
If string contains '\' then split string at '\' and extract username and domain
Else If string contains '@' then split string at '@' and extract username and domain
Else treat string as username without a domain

这就是我目前正在做的:
如果字符串包含 '\' 则在 '\' 处拆分字符串并提取用户名和域
Else 如果字符串包含 '@' 然后在 '@' 处拆分字符串并提取用户名和域
Else 将字符串视为没有域的用户名

I'm still hunting for a better solution in the case where the input string isn't in an easily predicted format, i.e. "domain\user@domain". I'm thinking RegEx...

在输入字符串不是易于预测的格式(即“域\用户@域”)的情况下,我仍在寻找更好的解决方案。我在想正则表达式...

Update: I stand corrected. My answer is a bit of out context, it refers to the general case of parsing username and domains out of user input, like in user login/logon prompt. Hope it still helps someone.

更新:我站纠正。我的回答有点脱离上下文,它指的是从用户输入中解析用户名和域的一般情况,例如在用户登录/登录提示中。希望它仍然可以帮助某人。

回答by StarCub

System.Environment.UserDomainNamegives you the domain name only

System.Environment.UserDomainName只给你域名

Similarly, System.Environment.UserNamegives you the user name only

同样,只System.Environment.UserName给你用户名

回答by Gruff Bunny

var components = User.Identity.Name.Split('\');

var userName = components.Last() 

var domainName = components.Reverse().Skip(1).FirstOrDefault()

回答by Adam Cooper

Seems like a problem made to be solved by regular expressions:

似乎是一个由正则表达式解决的问题:

public static class UserExtensions
{
    public static string GetDomain(this IIdentity identity)
    {
        Regex.Match(identity.Name, ".*\\").ToString()
    }

    public static string GetLogin(this IIdentity identity)
    {
        return Regex.Replace(identity.Name, ".*\\", "");
    }
}

回答by Mihai

Although not a .NET built-in, one can always P/Invoke to CredUIParseUserName. Here's a example of how to use it in .NET.

虽然不是 .NET 内置的,但始终可以 P/Invoke 到CredUIParseUserName下面是一个如何在 .NET 中使用它的示例。

PS: It doesn't seem to handle the "dot", as in ".\username".

PS:它似乎没有处理“点”,如“.\username”。