pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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

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

問題描述

我正在嘗試讓 ActiveDirectory 和標(biāo)準(zhǔn)表單登錄都能正常工作,但有一件事阻止了我.我無法獲取當(dāng)前 Windows 用戶的名稱.我得到的最接近的是 var i = WindowsIdentity.GetCurrent();,但這給了我 IIS 應(yīng)用程序池用戶的名稱.我在 IIS 中啟用了匿名身份驗證、表單身份驗證和 Windows 身份驗證.我可以從 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 中且它們不是匿名的時,這才有效.

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.

編輯:我想我誤解了您想要的內(nèi)容,因此這里對上述解決方案的情況進(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)閉匿名身份驗證并打開 Asp.Net Impersonation,IIS 將在有人訪問該站點時執(zhí)行 401 質(zhì)詢.
如果所有內(nèi)容都在同一個域中,Web 瀏覽器會將您的憑據(jù)發(fā)送到 IIS,IIS 將根據(jù)它的 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.

當(dāng)您打開 Asp.Net Impersonation 時,IIS 會將該標(biāo)識綁定到當(dāng)前線程/請求.因此,在身份驗證發(fā)生后,您可以從當(dāng)前線程標(biāo)識中獲取用戶名,然后像這樣查詢 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)前用戶名的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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 枚舉組時發(fā)生錯誤 (1301).升級到 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 組的成員,即包括子組)
主站蜘蛛池模板: CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | 压缩空气冷冻式干燥机_吸附式干燥机_吸干机_沪盛冷干机 | PCB设计,PCB抄板,电路板打样,PCBA加工-深圳市宏力捷电子有限公司 | 网站建设-高端品牌网站设计制作一站式定制_杭州APP/微信小程序开发运营-鼎易科技 | 播音主持培训-中影人教育播音主持学苑「官网」-中国艺考界的贵族学校 | 高低温万能试验机_拉力试验机_拉伸试验机-馥勒仪器科技(上海)有限公司 | 无尘烘箱_洁净烤箱_真空无氧烤箱_半导体烤箱_电子防潮柜-深圳市怡和兴机电 | 机床主轴维修|刀塔维修|C轴维修-常州翔高精密机械有限公司 | 南京PVC快速门厂家南京快速卷帘门_南京pvc快速门_世界500强企业国内供应商_南京美高门业 | 深圳希玛林顺潮眼科医院(官网)│深圳眼科医院│医保定点│香港希玛林顺潮眼科中心连锁品牌 | 欧版反击式破碎机-欧版反击破-矿山石料破碎生产线-青州奥凯诺机械 | 智成电子深圳tdk一级代理-提供TDK电容电感贴片蜂鸣器磁芯lambda电源代理经销,TDK代理商有哪些TDK一级代理商排名查询。-深圳tdk一级代理 | 台式低速离心机-脱泡离心机-菌种摇床-常州市万丰仪器制造有限公司 | 电梯装饰-北京万达中意电梯装饰有限公司| 订做不锈钢_不锈钢定做加工厂_不锈钢非标定制-重庆侨峰金属加工厂 | 天津蒸汽/热水锅炉-电锅炉安装维修直销厂家-天津鑫淼暖通设备有限公司 | EFM 022静电场测试仪-套帽式风量计-静电平板监测器-上海民仪电子有限公司 | 冷镦机-多工位冷镦机-高速冷镦机厂家-温州金诺机械设备制造有限公司 | 玉米加工设备,玉米深加工机械,玉米糁加工设备.玉米脱皮制糁机 华豫万通粮机 | 上海办公室装修公司_办公室设计_直营办公装修-羚志悦装 | 顺景erp系统_erp软件_erp软件系统_企业erp管理系统-广东顺景软件科技有限公司 | 高光谱相机-近红外高光谱相机厂家-高光谱成像仪-SINESPEC 赛斯拜克 | 并离网逆变器_高频UPS电源定制_户用储能光伏逆变器厂家-深圳市索克新能源 | ptc_浴霸_大巴_干衣机_呼吸机_毛巾架_电动车加热器-上海帕克 | 祝融环境-地源热泵多恒系统高新技术企业,舒适生活环境缔造者! | 基业箱_环网柜_配电柜厂家_开关柜厂家_开关断路器-东莞基业电气设备有限公司 | 承插管件_不锈钢承插管件_锻钢高压管件-温州科正阀门管件有限公司 | 贝朗斯动力商城(BRCPOWER.COM) - 买叉车蓄电池上贝朗斯商城,价格更超值,品质有保障! | 太原装修公司_山西整装家装设计_太原室内装潢软装_肖邦家居 | 粉末冶金注射成型厂家|MIM厂家|粉末冶金齿轮|MIM零件-深圳市新泰兴精密科技 | 塑胶跑道_学校塑胶跑道_塑胶球场_运动场材料厂家_中国塑胶跑道十大生产厂家_混合型塑胶跑道_透气型塑胶跑道-广东绿晨体育设施有限公司 | 精密模具-双色注塑模具加工-深圳铭洋宇通 | 合肥白癜风医院_合肥治疗白癜风医院_合肥看白癜风医院哪家好_合肥华研白癜风医院 | 逗网红-抖音网红-快手网红-各大平台网红物品导航 | 砍排机-锯骨机-冻肉切丁机-熟肉切片机-预制菜生产线一站式服务厂商 - 广州市祥九瑞盈机械设备有限公司 | 武汉森源蓝天环境科技工程有限公司-为环境污染治理提供协同解决方案 | 郑州大巴车出租|中巴车租赁|旅游大巴租车|包车|郑州旅游大巴车租赁有限公司 | 自动螺旋上料机厂家价格-斗式提升机定制-螺杆绞龙输送机-杰凯上料机 | 防腐木批发价格_深圳_惠州_东莞防腐木厂家_森源(深圳)防腐木有限公司 | 尼龙PA610树脂,尼龙PA612树脂,尼龙PA1010树脂,透明尼龙-谷骐科技【官网】 | 天空彩票天下彩,天空彩天空彩票免费资料,天空彩票与你同行开奖,天下彩正版资料大全 |