久久久久久久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激情视频 | 国产午夜激情 | 免费观看全黄做爰的视频 | 免费av网站在线观看 | 黄色日批视频 | 亚洲国产精品久久久久久久 | 日韩av资源 | 久久精品国产免费 | 欧美精品黄色 | 国产天堂网 | 亚洲精品综合 | 国产小视频在线 | 亚洲精品乱码久久久久久动漫 | 国产伦精品一区二区 | 日韩综合精品 | txvlog.com| 日韩在线免费观看视频 | 国产午夜精品视频 | 日韩一区二区在线观看视频 | 欧美激情在线播放 | 国内自拍偷拍 | 麻豆一区二区三区四区 | 久久不射网 | 黄色免费小视频 | www.中文字幕.com | 国产农村妇女aaaaa视频 | 特一级黄色片 | 婷婷综合久久 | aaa免费视频 | 国产午夜麻豆影院在线观看 | 欧美视频精品 | 欧美黄色网 | 亚洲高清免费视频 | 深夜福利视频网站 | 黄免费视频 | 免费看的黄色片 | 一级片在线视频 | 中文字幕在线观看日韩 | 精品久久免费 |