久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

ASP.NET 中的 ActiveDirectory 當前用戶名

ActiveDirectory Current Username in ASP.NET(ASP.NET 中的 ActiveDirectory 當前用戶名)
本文介紹了ASP.NET 中的 ActiveDirectory 當前用戶名的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試讓 ActiveDirectory 和標準表單登錄都能正常工作,但有一件事阻止了我.我無法獲取當前 Windows 用戶的名稱.我得到的最接近的是 var i = WindowsIdentity.GetCurrent();,但這給了我 IIS 應用程序池用戶的名稱.我在 IIS 中啟用了匿名身份驗證、表單身份驗證和 Windows 身份驗證.我可以從 AD 加載用戶,所以我假設我的 web.config 設置正確.

I'm trying to get both ActiveDirectory and standard forms login working but one thing is stopping me. I can't get the name of the current windows user. The closest I've got is var i = WindowsIdentity.GetCurrent();, but that gives me the name of the IIS app pool user. I have Anonymous Authentication, Forms Authentication and Windows Authentication enabled in IIS. I can load users from AD so I assume my web.config is setup correctly.

編輯:這是我的 web.config(使用 Facade 提供程序):

Edit: This is my web.config (using a Facade provider):

<membership defaultProvider="HybridMembershipProvider">
      <providers>
        <clear />
        <add name="HybridMembershipProvider" type="MyApp.Data.HybridMembershipProvider" AspNetProviderName="AspNetSqlMembershipProvider" ActiveDirectoryProviderName="ADMembershipProvider" />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="MyAppConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
        <add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ADConnectionString" 
            attributeMapUsername="sAMAccountName" enableSearchMethods="true" attributeMapEmail="mail"/>
      </providers>
    </membership>

編輯 2:這是我的 IIS 安全設置.

Edit 2: Here's my IIS security setup.

推薦答案

如果您在 IIS 中打開 ASP.Net Impersonation,您可以獲得您想要的用戶名.僅當該數據位于表單成員資格提供商/AD 中且它們不是匿名的時,這才有效.

If you turn on ASP.Net Impersonation in IIS, you can get the username like you wanted to. This will only work if that data is in the forms membership provider / AD, and they are not Anonymous.

此外,混合基于表單和基于 Windows/AD 的身份驗證是可行的,但不推薦.如果需要,請參閱此.

Also, mixing Forms based and Windows/AD based auth is doable but not recommended. See this if you need to do it.

編輯:我想我誤解了您想要的內容,因此這里對上述解決方案的情況進行了高層次的掩飾:

EDIT: I think I misunderstood what you wanted so here's a high-level glossing over of what goes on with the aforementioned solution:

如果您關閉匿名身份驗證并打開 Asp.Net Impersonation,IIS 將在有人訪問該站點時執行 401 質詢.
如果所有內容都在同一個域中,Web 瀏覽器會將您的憑據發送到 IIS,IIS 將根據它的 Active Directory 驗證它們,然后 AD 將為 IIS 提供一個身份以供使用.

If you turn off Anonymous Authentication, and turn on Asp.Net Impersonation, IIS will do a 401 Challenge whenever somebody visits the site.
If everything is on the same domain, the web browser will send your credentials to IIS, IIS will validate them against it's Active Directory, and then AD will give IIS an Identity to work with.

當您打開 Asp.Net Impersonation 時,IIS 會將該標識綁定到當前線程/請求.因此,在身份驗證發生后,您可以從當前線程標識中獲取用戶名,然后像這樣查詢 Active Directory:

When you have Asp.Net Impersonation turned on, IIS will then bind that Identity to the current thread/request. So after authentication happens, you can just grab the username from the current thread identity, and then query Active Directory like:

using System.Threading;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;

......

PrincipalContext pc = null;
UserPrincipal principal = null;

try
{
    var username = Thread.CurrentPrincipal.Identity.Name;
    pc = new PrincipalContext(ContextType.Domain, "active.directory.domain.com");
    principal = UserPrincipal.FindByIdentity(pc, username);

    var firstName = principal.GivenName ?? string.Empty
    var lastName = principal.Surname ?? string.Empty
    return string.Format("Hello {0} {1}!", firstName, lastName);
}
catch ...
finally
{
    if (principal != null) principal.Dispose();
    if (pc != null) pc.Dispose();
}

這篇關于ASP.NET 中的 ActiveDirectory 當前用戶名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Asp.net System.Web.HttpContext.Current.Session null in global.asax(Asp.net System.Web.HttpContext.Current.Session 在 global.asax 中為 null)
Caught exception is null itself !(捕獲的異常本身為空!)
Is an empty textbox considered an empty string or null?(空文本框被視為空字符串還是 null?)
UserPrincipals.GetAuthorizationGroups An error (1301) occurred while enumerating the groups. After upgrading to Server 2012 Domain Controller(UserPrincipals.GetAuthorizationGroups 枚舉組時發生錯誤 (1301).升級到 Server 2012 域控制
Using PrincipalSearcher to find users with quot;orquot; parameters(使用 PrincipalSearcher 查找帶有“或的用戶參數)
Get members of an Active Directory group recursively, i.e. including subgroups(遞歸獲取 Active Directory 組的成員,即包括子組)
主站蜘蛛池模板: 成人精品视频 | 99久久久无码国产精品 | 蜜桃毛片 | 日韩欧美在线视频观看 | 一级免费毛片 | 日韩精品在线免费观看 | 亚洲在线一区二区 | 九九九久久国产免费 | 色天天综合 | 精品国产91 | 日韩中文字幕网 | 国产区一区二区三区 | 请别相信他免费喜剧电影在线观看 | 色毛片| 欧美日韩久久久久 | 狠狠操天天干 | 国产特级毛片 | 99久久精品国产一区二区三区 | 成人免费影院 | 国产精品99久久久久久动医院 | 久久影音先锋 | 午夜精品久久久久99蜜 | 男人亚洲天堂 | 亚洲日本欧美 | 欧美女优在线观看 | 欧美日韩一区二区三区四区 | 91美女视频 | 亚洲福利一区二区 | 色综合久久久久 | 天天干天天操天天爽 | 99精品电影 | 国产在线中文 | 国产一区久久久 | 九九久久精品视频 | 色性av | 青青草综合 | www.亚洲.com| 国产自产c区| 国产激情一区二区三区 | 成人午夜精品 | 欧美黑人体内she精在线观看 |