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

ASP.NET 中的 ActiveDirectory 當(dāng)前用戶名

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

問題描述

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

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 安全設(shè)置.

Edit 2: Here's my IIS security setup.

推薦答案

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

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 的身份驗(yàn)證是可行的,但不推薦.如果需要,請(qǐng)參閱此.

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

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

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

如果您關(guān)閉匿名身份驗(yàn)證并打開 Asp.Net Impersonation,IIS 將在有人訪問該站點(diǎn)時(shí)執(zhí)行 401 質(zhì)詢.
如果所有內(nèi)容都在同一個(gè)域中,Web 瀏覽器會(huì)將您的憑據(jù)發(fā)送到 IIS,IIS 將根據(jù)它的 Active Directory 驗(yàn)證它們,然后 AD 將為 IIS 提供一個(gè)身份以供使用.

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.

當(dāng)您打開 Asp.Net Impersonation 時(shí),IIS 會(huì)將該標(biāo)識(shí)綁定到當(dāng)前線程/請(qǐng)求.因此,在身份驗(yàn)證發(fā)生后,您可以從當(dāng)前線程標(biāo)識(shí)中獲取用戶名,然后像這樣查詢 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();
}

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

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

相關(guān)文檔推薦

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 枚舉組時(shí)發(fā)生錯(cuò)誤 (1301).升級(jí)到 Server 2012 域控制
Using PrincipalSearcher to find users with quot;orquot; parameters(使用 PrincipalSearcher 查找?guī)в小盎虻挠脩魠?shù))
Get members of an Active Directory group recursively, i.e. including subgroups(遞歸獲取 Active Directory 組的成員,即包括子組)
主站蜘蛛池模板: 欧美高清一区 | 91五月婷蜜桃综合 | 在线视频中文字幕 | 国产日韩欧美 | 日韩在线精品 | 精品久久久久久久 | 亚洲国产一区二区三区 | 成人国产精品久久久 | 久久99深爱久久99精品 | www.天天操| 亚洲一区二区三区四区五区中文 | 成人福利在线 | 欧美一级二级视频 | 成人毛片视频免费 | 亚洲欧美成人影院 | 国产在线中文字幕 | 日韩中出 | 日韩一区二区三区四区五区六区 | 一级毛片中国 | 2023亚洲天堂 | 亚洲午夜视频 | 国产亚洲一区二区三区 | 精品国产一区二区三区久久狼黑人 | 日本韩国电影免费观看 | 日韩av高清在线 | 久久国产欧美日韩精品 | 欧美精品一区二区三区蜜桃视频 | 热99在线 | 亚洲日韩中文字幕 | 亚洲欧洲中文 | 久久国产精品色av免费观看 | 欧美精品一区在线观看 | 欧美日一区| 一二三四在线视频观看社区 | 久久国产精品首页 | 91免费在线视频 | 国产精品高潮呻吟久久 | 久久一| 亚洲综合第一页 | 精品日韩一区 | 国产一区二区三区免费视频 |