Azure 身份验证重定向到不需要的 URL

时间:2021-02-03 15:02:37

标签: azure webforms azure-active-directory azure-authentication

我正在构建 Azure AD 身份验证,假设我的应用程序 url 是 https://hostname/applicationName/Default.aspx,因此在点击此 url 后,用户将被重定向到 Azure 登录。 问题是,即使用户不属于 AD 组,他们仍然会被重定向到应用程序的默认页面,而不是抛出无效的用户消息。

有人可以让我知道在哪里寻找,下面是我在 app startup.cs 中使用的代码片段:

    private static string clientId = ConfigurationManager.AppSettings["ClientId"];
    private static string aadInstance = EnsureTrailingSlash(ConfigurationManager.AppSettings["Authority"]);
    private static string tenantId = ConfigurationManager.AppSettings["Tenant"];
    private static string postLogoutRedirectUri = ConfigurationManager.AppSettings["redirectUri"];

    string authority = aadInstance + tenantId;


    public void Configuration(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        app.UseKentorOwinCookieSaver();
        app.UseCookieAuthentication(new CookieAuthenticationOptions());
        app.UseOpenIdConnectAuthentication(
            new OpenIdConnectAuthenticationOptions
            {
        ClientId = clientId,
                Authority = authority,
                RedirectUri = postLogoutRedirectUri,
        PostLogoutRedirectUri = postLogoutRedirectUri,
                Scope = OpenIdConnectScope.OpenIdProfile,
        ResponseType = OpenIdConnectResponseType.IdToken,
        TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuer = true 
        },
        Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    AuthenticationFailed = OnAuthenticationFailed
                }
            }
        );
    }

我的 web.config 文件中的 redirectUri 是 https://hostname/applicationName/Default.aspx

在我的应用程序主页中,Page_Load() 中有以下代码

        if (!Page.IsPostBack)
        {

            if (!Request.IsAuthenticated)
            {
                HttpContext.Current.GetOwinContext().Authentication.Challenge(
                    new AuthenticationProperties { RedirectUri = ConfigurationManager.AppSettings["redirectUri"] }, 
                    OpenIdConnectAuthenticationDefaults.AuthenticationType);
// Is this RedirectUri property correct?
            }
            //Application code
       }

我在 startup.cs 中设置了以下内容

TokenValidationParameters = new TokenValidationParameters()
            {
                   ValidateIssuer = true 
            }

如果需要任何其他数据,请告诉我。

2 个答案:

答案 0 :(得分:0)

根据您的描述“如果用户不属于 AD 组,他们仍将被重定向到应用程序的默认页面”,我假设您可以设置 'ConfigurationManager.AppSettings["Tenant"]; 的值; '作为“常见”。

我已经在我的地方进行了测试,当我设置了组织的租户 ID 时,我无法使用外部用户登录。 'common' 的值意味着任何用户都可以访问该应用程序,这样我就可以使用刚才无法登录的用户帐户成功登录。

如果它不起作用,请告诉我。

enter image description here

答案 1 :(得分:0)

根据您要实现的场景,您有多种选择。

如果您想要的是不属于您的 Azure AD 租户的用户无法登录,那么您应该确保租户属性不是“公共”,而是您自己的租户 ID,您可以从 Azure 门户获取,如下所示 enter image description here

如果您想要实现的是一个更复杂的基于组的 AuthZ 过程,您可以参考来自 Microsoft 的此 sample 以了解所有详细信息