当callbackurl是目录时Auth0身份验证不起作用

时间:2018-08-22 10:49:08

标签: asp.net-mvc auth0

我很难找到我认为适合Google的单词。我正在将Auth0集成到一个新的网站中,并且遵循AS.NET MVC网站的快速入门教程。

当我将“允许的回调”设置为https://localhost:44334时,所有内容都像一个超级按钮。我可以登录,看到所有创建的cookie。

但是,当我将允许的回调设置为https://localhost:44334/anyFolderHere时,我什么也没得到。该网站重定向到Auth0,我可以登录,然后重定向到正确的位置。但是没有cookie,没有auth令牌,什么也没有。

我在Auth0日志中看到它认为一切正常,登录成功。我看不到代码中抛出任何错误,我什么也没得到。

从我的快速入门开始,我所拥有的代码几乎是逐行的。我不愿意开始进行更改,直到看到它起作用为止。

我确定我在这里犯了一个菜鸟错误,但是我感到沮丧,甚至看不见自己。

这是我所拥有的:

public class Startup
{
    /// <summary>
    /// Configure OWIN to use OpenIdConnect 
    /// </summary>
    /// <param name="app"></param>
    public void Configuration(IAppBuilder app)
    {
        // Configure Auth0 parameters
        string auth0Domain = System.Configuration.ConfigurationManager.AppSettings["auth0:Domain"];
        string auth0ClientId = System.Configuration.ConfigurationManager.AppSettings["auth0:ClientId"];
        string auth0ClientSecret = System.Configuration.ConfigurationManager.AppSettings["auth0:ClientSecret"];
        string auth0RedirectUri = System.Configuration.ConfigurationManager.AppSettings["auth0:RedirectUri"];
        string auth0PostLogoutRedirectUri = System.Configuration.ConfigurationManager.AppSettings["auth0:PostLogoutRedirectUri"];

        // Enable the Cookie saver middleware to work around a bug in the OWIN implementation
        app.UseKentorOwinCookieSaver();

        // Set Cookies as default authentication type
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
            LoginPath = new PathString("/Account/Login")
        });

        // Configure Auth0 authentication
        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            AuthenticationType = "Auth0",

            Authority = $"https://{auth0Domain}",

            ClientId = auth0ClientId,
            ClientSecret = auth0ClientSecret,

            RedirectUri = auth0RedirectUri,
            PostLogoutRedirectUri = auth0PostLogoutRedirectUri,

            ResponseType = OpenIdConnectResponseType.CodeIdToken,
            Scope = "openid profile",

            TokenValidationParameters = new TokenValidationParameters
            {
                NameClaimType = "name"
            },

            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                RedirectToIdentityProvider = notification =>
                {
                    if (notification.ProtocolMessage.RequestType == OpenIdConnectRequestType.Logout)
                    {
                        var logoutUri = $"https://{auth0Domain}/v2/logout?client_id={auth0ClientId}";

                        var postLogoutUri = notification.ProtocolMessage.PostLogoutRedirectUri;
                        if (!string.IsNullOrEmpty(postLogoutUri))
                        {
                            if (postLogoutUri.StartsWith("/"))
                            {
                                // transform to absolute
                                var request = notification.Request;
                                postLogoutUri = request.Scheme + "://" + request.Host + request.PathBase + postLogoutUri;
                            }
                            logoutUri += $"&returnTo={ Uri.EscapeDataString(postLogoutUri)}";
                        }

                        notification.Response.Redirect(logoutUri);
                        notification.HandleResponse();
                    }
                    return Task.FromResult(0);
                }
            }
        });
    }
}


public class AccountController : Controller
    {
        public ActionResult Login(string returnUrl)
        {
            HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties
                {
                    RedirectUri = returnUrl ?? Url.Action("Index", "Home")
                },
                "Auth0");
            return new HttpUnauthorizedResult();
        }

        [Authorize]
        public void Logout()
        {
            HttpContext.GetOwinContext().Authentication.SignOut(CookieAuthenticationDefaults.AuthenticationType);
            HttpContext.GetOwinContext().Authentication.SignOut("Auth0");
        }
    }

编辑:我为AuthenticationFailed,AuthorizationCodeReceived,MessageReceived,SecurityTokenReceived和SecurityTokenValidated添加了事件处理程序。调用https:// {host} / anyFolderHere时,这些句柄不会触发。但是,当它们仅来自https:// {host} /时便会触发。

添加了代码,在调试器的每一步都有断点。

 },AuthenticationFailed = failed =>
                    {
                        return Task.FromResult(0);
                    },
                    AuthorizationCodeReceived = received =>
                    {
                        return Task.FromResult(0);
                    },
                    MessageReceived = message =>
                    {
                        return Task.FromResult(0);
                    },
                    SecurityTokenReceived = token =>
                    {
                        return Task.FromResult(0);
                    },
                    SecurityTokenValidated = validate =>
                    {
                        return Task.FromResult(0);
                    }

0 个答案:

没有答案