如何获得无域用户名 [英] How to get username without domain

查看:204
本文介绍了如何获得无域用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个aspx页我得到的功能 Request.LogonUserIdentity.Name Windows的用户名。该函数返回的格式为域\\用户的字符串。

有一些功能只获取用户名,而不诉诸的IndexOf 子串,这样吗?

 公共静态字符串StripDomain(字符串的用户名)
{
    INT POS = username.IndexOf('\\\\');
    返回POS!= -1? username.Substring(POS + 1):用户名;
}


解决方案

我不这么认为。我有使用这些方法之前,用户名 -

  System.Security.Principal.IPrincipal用户= System.Web.HttpContext.Current.User;
System.Security.Principal.IIdentity身份= user.Identity;
返回identity.Name.Substring(identity.Name.IndexOf(@\\)+ 1);

<$p$p><$c$c>Request.LogonUserIdentity.Name.Substring(Request.LogonUserIdentity.Name.LastIndexOf(@\"\\\") + 1);

In an aspx page I get the Windows username with the function Request.LogonUserIdentity.Name. This function returns a string in the format "domain\user".

Is there some function to only get the username, without resorting to the IndexOf and Substring, like this?

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

解决方案

I don't believe so. I have got the username using these methods before-

System.Security.Principal.IPrincipal user = System.Web.HttpContext.Current.User;   
System.Security.Principal.IIdentity identity = user.Identity;  
return identity.Name.Substring(identity.Name.IndexOf(@"\") + 1);

or

Request.LogonUserIdentity.Name.Substring(Request.LogonUserIdentity.Name.LastIndexOf(@"\") + 1);

这篇关于如何获得无域用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆