简体   繁体   English

如何在没有域的情况下获取用户名

[英]How to get username without domain

In an aspx page I get the Windows username with the function Request.LogonUserIdentity.Name .在 aspx 页面中,我使用Request.LogonUserIdentity.Name函数获取 Windows 用户名。 This function returns a string in the format "domain\\user\u0026quot;.此函数返回格式为“域\\用户”的字符串。

Is there some function to only get the username, without resorting to the IndexOf and Substring , like this?是否有一些函数只获取用户名,而不使用IndexOfSubstring ,像这样?

public static string StripDomain(string username)
{
    int pos = username.IndexOf('\\');
    return pos != -1 ? username.Substring(pos + 1) : username;
}

If you are using Windows Authentication.如果您使用的是 Windows 身份验证。 This can simply be achieved by calling System.Environment.UserName which will give you the user name only.这可以简单地通过调用System.Environment.UserName来实现,它只会为您提供用户名。 If you want only the Domain name you can use System.Environment.UserDomainName如果你只想要域名,你可以使用System.Environment.UserDomainName

I don't believe so.我不相信。 I have got the username using these methods before-我之前使用这些方法获得了用户名-

var user = System.Web.HttpContext.Current.User;   
var name = user.Identity.Name;

var slashIndex = name.IndexOf("\\");
return slashIndex > -1 
    ? name.Substring(slashIndex  + 1)
    : name.Substring(0, name.IndexOf("@"));

or或者

var name = Request.LogonUserIdentity.Name;

var slashIndex = name.IndexOf("\\");
return slashIndex > -1 
    ? name.Substring(slashIndex  + 1)
    : name.Substring(0, name.IndexOf("@"));

Getting parts[1] is not a safe approach.获取部件 [1] 不是一种安全的方法。 I would prefer use LINQ .Last():我更喜欢使用 LINQ .Last():

WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
if (windowsIdentity == null)
    throw new InvalidOperationException("WindowsIdentity is null");
string nameWithoutDomain = windowsIdentity.Name.Split('\\').Last();

If you are using .NET 3.5 you could always create an extension method to the WindowsIdentity class that does this work for you.如果您使用的是 .NET 3.5,您总是可以为 WindowsIdentity 类创建一个扩展方法来为您完成这项工作。

public static string NameWithoutDomain( this WindowsIdentity identity )
{
    string[] parts = identity.Name.Split(new char[] { '\\' });

    //highly recommend checking parts array for validity here 
    //prior to dereferencing

    return parts[1];
}

that way all you have to do anywhere in your code is reference:这样你在代码中的任何地方要做的就是引用:

Request.LogonUserIdentity.NameWithoutDomain(); Request.LogonUserIdentity.NameWithoutDomain();

static class IdentityHelpers
{
    public static string ShortName(this WindowsIdentity Identity)
    {
        if (null != Identity)
        {
            return Identity.Name.Split(new char[] {'\\'})[1];
        }
        return string.Empty;
    }
}

If you include this code, you could then just do something like:如果包含此代码,则可以执行以下操作:

WindowsIdentity a = WindowsIdentity.GetCurrent();
Console.WriteLine(a.ShortName);

Obviously in a web environment, you wouldn't write to the console - just an example...显然,在网络环境中,您不会写入控制台 - 只是一个例子......

An alternative way of doing the same thing as the other answers:做与其他答案相同的事情的另一种方法:

var usernameWithoutDomain = Path.GetFileName(@"somedomain\someusername")

It is just unsafe as not checking for the @ variant of usernames.不检查用户名的@ 变体是不安全的。

I was suggesting to use regexpes but they would be overkill.我建议使用正则表达式,但它们会矫枉过正。 [System.String.Split]( http://msdn.microsoft.com/en-us/library/b873y76a(VS.80).aspx) do the job. [System.String.Split]( http://msdn.microsoft.com/en-us/library/b873y76a(VS.80).aspx)完成这项工作。

string[] parts= username.Split( new char[] {'\\'} );
return parts[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM