mse*_*dio 6 .net c# asp.net iis-7.5 userprincipal
以下代码仅适用于IIS中仅为我们网络上的本地用户启用Windows身份验证的情况.
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
UserPrincipal up = UserPrincipal.FindByIdentity(ctx, userName);
return up;
}
Run Code Online (Sandbox Code Playgroud)
否则会抛出此异常:
[ArgumentException :(&(objectCategory = user)(objectClass = user)(|(userPrincipalName =)(distinguishedName =)(name =)))搜索过滤器无效.] System.DirectoryServices.ResultsEnumerator.MoveNext()+434305 System .DirectoryServices.SearchResultCollection.get_InnerList()+282 System.DirectoryServices.SearchResultCollection.get_Count()+ 9 System.DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRefHelper(Type principalType,String urnScheme,String urnValue,DateTime referenceDate,Boolean useSidHistory)+1898 System. DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(类型principalType,String urnScheme,String urnValue,DateTime referenceDate)+85 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context,Type principalType,Nullable`1 identityType,String identityValue,DateTime refDate)+ 211 System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context,String identi)tyValue)+95 WebApplication1.Index.GetUserPrincipal(String userName)在C:\ Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Index.aspx.cs:38 WebApplication1.Index.Page_Load(Object sender,EventArgs) e)在C:\ Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Index.aspx.cs:19 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp,Object o,Object t,EventArgs e) )+25 System.Web.UI.Control.LoadRecursive()+71 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)+3064
是否有任何方法可以让这个用于获取本地用户UserPrincipal 而Windows和匿名身份验证都打开?
不确定如何让 FindByIdentity 工作,因为我认为还需要指定身份类型?IE:
UserPrincipal up = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userName);
Run Code Online (Sandbox Code Playgroud)
不管怎样,如果你强迫的话,模仿可能会起作用。因此,在该代码片段之前使用以下内容:
// This will impersonate the logged in user in order to get whichever username you require GIVEN the logged in user has AD read/querying rights.
System.Web.HttpContext.Current.Request.LogonUserIdentity.Impersonate();
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
UserPrincipal up = UserPrincipal.FindByIdentity(ctx, userName);
return up;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6447 次 |
| 最近记录: |