![](/img/trans.png)
[英]System.Web.HttpContext.Current.User.Identity.Name Vs System.Environment.UserName in ASP.NET
[英]System.Web.HttpContext.Current.User.Identity.Name not consistent
在我們的項目中,我們使用System.Web.HttpContext.Current.User.Identity.Name來獲取具有Domain的UserID。 對於某些頁面,例如登錄頁面或主頁頁面,或者其中許多頁面,它會按預期返回用戶名,其中domainName為
域名\\用戶ID
當我們想嘗試在頁面之一上使用Usercontrol上傳文件時,它會拋出異常,因為它直接返回了Username
像Harry,Burlington而不是dom \\ Hburlingt。
我們在Web,配置和示例代碼中使用Windows身份驗證模式,這是拋出異常的原因
public static string GetCurrUsersLoginIdentity()
{
string name = "";
if (System.Web.HttpContext.Current != null)
name = System.Web.HttpContext.Current.User.Identity.Name;
else
{
WindowsIdentity id = WindowsIdentity.GetCurrent();
if (id != null)
name = id.Name;
}
//If we still don't have a name then something is seriously wrong
if (string.IsNullOrEmpty(name))
throw new System.Security.Authentication.InvalidCredentialException("Coult not retrieve IDSID information for user.");
return name;
User.Identity
和WindowsIdentity.GetCurrent()
是兩件事:
MSDN上的User.Identity返回System.Security.Principal
對象。
MSDN上的WindowsIdentity.GetCurrent返回一個System.Security.Principal.IPrincipal
對象。
因此,看到兩個不同的名稱也就不足為奇了。
順便說一句,您的方法GetCurrUsersLoginIdentity
的名稱具有誤導性,因為它返回的是用戶名而不是身份對象本身。
而且,您的異常消息中包含拼寫錯誤: Coult not retrieve IDSID information for user.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.