【问题标题】:How to allow my Azure web application to access Active Directory?如何允许我的 Azure Web 应用程序访问 Active Directory?
【发布时间】:2018-10-09 15:56:17
【问题描述】:

我有一个使用 Active Directory 进行身份验证和授权的 WebForms 应用程序。它在 localhost 上工作得很好,但是当我尝试访问发布到 Azure 的站点时失败。我已确保该应用程序已在 Azure Active Directory 中注册。如果我不尝试获取当前用户的 AD 安全组,该应用程序将按预期启动并运行。以下是导致问题的 Site Master 的 Page_Load 事件中的代码:

protected void Page_Load(object sender, EventArgs e)
    {
        PrincipalSearchResult<Principal> groups = UserPrincipal.Current.GetAuthorizationGroups();

        IEnumerable<string> groupNames = groups.Select(x => x.Name);

        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
            HyperLink newphaud = LIV.FindControl("liaNewPhaud") as HyperLink;
            HtmlGenericControl liadmin = LIV.FindControl("navAdminsDdl") as HtmlGenericControl;

            newphaud.Visible = (groupNames.Contains("SG1") || groupNames.Contains("SG2"));

            liadmin.Visible = groupNames.Contains("SG1");
        }
    }

以下是异常详情:

System.Runtime.InteropServices.COMException:访问被拒绝。

这是堆栈跟踪:

[COMException (0x80070005): Access is denied.
]
   System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +399115
   System.DirectoryServices.DirectoryEntry.Bind() +36
   System.DirectoryServices.DirectoryEntry.RefreshCache() +45
   System.DirectoryServices.AccountManagement.PrincipalContext.DoMachineInit() +211
   System.DirectoryServices.AccountManagement.PrincipalContext.Initialize() +128
   System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx() +31
   System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate) +14
   System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, IdentityType identityType, String identityValue) +90
   System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, IdentityType identityType, String identityValue) +32
   System.DirectoryServices.AccountManagement.UserPrincipal.get_Current() +191
   phaud.SiteMaster.Page_Load(Object sender, EventArgs e) in C:\Users\user1\source\repos\phaud\phaud\Site.Master.cs:19
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +95
   System.Web.UI.Control.LoadRecursive() +59
   System.Web.UI.Control.LoadRecursive() +131
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678

我需要做什么才能让我的 Azure Web 应用访问 AD?

【问题讨论】:

    标签: asp.net azure active-directory


    【解决方案1】:

    请注意 Azure AD != Windows AD。从代码看来,您的应用程序正在访问本地 Windows AD。因此,当您在 Azure 中时,您基本上有两种选择 1) 使用 Azure AD API 进行身份验证并获取用户信息 2) 您可以有一个单独的 Windows VM 运行 Windows AD,然后您的 Azure Web 应用程序将访问它。

    如果我是你,我会探索第一个选项。希望对你有帮助

    【讨论】:

    • 这绝对有帮助。我将探讨第一个选项。我认为既然它们已同步,我可以使用相同的代码,但这绝对不准确。
    猜你喜欢
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 2020-12-10
    • 2019-03-13
    • 1970-01-01
    • 2016-12-20
    相关资源
    最近更新 更多