6

次のコードは、ネットワーク上のローカル ユーザーに対して IIS で Windows 認証のみが有効になっている場合にのみ機能します。

using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
     UserPrincipal up = UserPrincipal.FindByIdentity(ctx, userName);
     return up;
}

それ以外の場合は、次の例外がスローされます。

[ArgumentException: (&(objectCategory=user)(objectClass=user)(|(userPrincipalName=)(distinguishedName=)(name=))) 検索フィルターが無効です。] System.DirectoryServices.ResultsEnumerator.MoveNext() +434305 システム.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 DirectoryServices.AccountManagement.ADStoreCtx.FindPrincipalByIdentRef(型 principalType、文字列 urnScheme、文字列 urnValue、DateTime referenceDate) +85 System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext コンテキスト、型 principalType、Nullable`1 identityType, String identityValue, DateTime refDate) +211 System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) +95 WebApplication1.Index.GetUserPrincipal(String userName) in C:\Users\xxx\Documents\ C:\Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Index の Visual Studio 2010\Projects\WebApplication1\WebApplication1\Index.aspx.cs:38 WebApplication1.Index.Page_Load(オブジェクト送信者、EventArgs e) .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 (ブール値 includeStagesBeforeAsyncPoint、ブール値 includeStagesAfterAsyncPoint) +3064DateTime refDate) +211 System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) +95 WebApplication1.Index.GetUserPrincipal(String userName) in C:\Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\ WebApplication1\Index.aspx.cs:38 C:\Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Index.aspx.cs:19 システム内の WebApplication1.Index.Page_Load(オブジェクト送信者、EventArgs e)。 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 ) +3064DateTime refDate) +211 System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue) +95 WebApplication1.Index.GetUserPrincipal(String userName) in C:\Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\ WebApplication1\Index.aspx.cs:38 C:\Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Index.aspx.cs:19 システム内の WebApplication1.Index.Page_Load(オブジェクト送信者、EventArgs e)。 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 ) +3064C:\Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Index.aspx.cs:38 WebApplication1.Index. C:\Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Index.aspx.cs:19 の Page_Load(Object sender, EventArgs e) 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) +3064C:\Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Index.aspx.cs:38 WebApplication1.Index. C:\Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Index.aspx.cs:19 の Page_Load(Object sender, EventArgs e) 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\Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Index.aspx.cs:38 C:\Users\xxx\Documents\Visual Studio 2010\ の WebApplication1.Index.Page_Load (オブジェクト送信者、EventArgs e) 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 (ブール値 includeStagesBeforeAsyncPoint、ブール値 includeStagesAfterAsyncPoint) +3064\Users\xxx\Documents\Visual Studio 2010\Projects\WebApplication1\WebApplication1\Index.aspx.cs:38 C:\Users\xxx\Documents\Visual Studio 2010\ の WebApplication1.Index.Page_Load (オブジェクト送信者、EventArgs e) 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 (ブール値 includeStagesBeforeAsyncPoint、ブール値 includeStagesAfterAsyncPoint) +3064Control.LoadRecursive() +71 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3064Control.LoadRecursive() +71 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3064

Windows 認証と匿名認証の両方がオンになっているときに、ローカル ユーザーの UserPrincipal を取得するためにこれを機能させる方法はありますか?

4

2 に答える 2

1

userName空の文字列 (または、他の方法で完全に空白で構成されている) である必要があり、明らかにそれは によって検証されていませんFindByIdentity

于 2013-04-09T14:31:21.123 に答える
0

ID タイプも指定する必要があると思ったので、FindByIdentity をどのように機能させたのかわかりませんか? すなわち:

UserPrincipal up = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, userName);

いずれにせよ、強制すると偽装が機能する可能性があります。したがって、そのコード スニペットの前に、次を使用します。

// 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;
    }
于 2013-04-09T14:25:41.647 に答える