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

问题描述

我正在构建 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 
            }

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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)