Auth0和Asp.Net Core 2.0 Razor页面登录路径问题

Auth0和Asp.Net Core 2.0 Razor页面登录路径问题,razor,auth0,asp.net-core-2.0,Razor,Auth0,Asp.net Core 2.0,我正在创建一个Razor Pages应用程序,Auth0作为身份验证提供程序,我遇到了登录路径问题。我看到其他StackOverflow回答说,您应该将其放入ConfigureServices方法中: services.ConfigureApplicationCookie(options => options.LoginPath = "/Index/Login"); 我试着把它放在代码的services.AddAuthentication部分下面,但它没有重定向到/Index/Login

我正在创建一个Razor Pages应用程序,Auth0作为身份验证提供程序,我遇到了登录路径问题。我看到其他StackOverflow回答说,您应该将其放入ConfigureServices方法中:

services.ConfigureApplicationCookie(options => options.LoginPath = "/Index/Login");
我试着把它放在代码的services.AddAuthentication部分下面,但它没有重定向到/Index/Login。我在其他任何地方都看不到如何正确获得[Authorize]属性以重定向到Auth0登录页面。我想如果我可以将路径设置为索引页,那么代码将运行:

public async void OnGetLogin(string returnUrl = "/")
{
  await HttpContext.ChallengeAsync("Auth0", new AuthenticationProperties() { RedirectUri = returnUrl });
}
我的完整配置服务代码是:

public void ConfigureServices(IServiceCollection services)
{
    // Add authentication services
    services.AddAuthentication(options => {
        options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;                 
    })
    .AddCookie()
    .AddOpenIdConnect("Auth0", options => {
        // Set the authority to your Auth0 domain
        options.Authority = $"https://{Configuration["Auth0:Domain"]}";

        // Configure the Auth0 Client ID and Client Secret
        options.ClientId = Configuration["Auth0:ClientId"];
        options.ClientSecret = Configuration["Auth0:ClientSecret"];

        // Set response type to code
        options.ResponseType = "code";

        // Configure the scope
        options.Scope.Clear();
        options.Scope.Add("openid");
        options.Scope.Add("groups");
        options.Scope.Add("profile");
        options.Scope.Add("email");                                

        // Set the callback path, so Auth0 will call back to http://localhost:5000/signin-auth0 
        // Also ensure that you have added the URL as an Allowed Callback URL in your Auth0 dashboard 
        options.CallbackPath = new PathString("/signin-auth0");

        // Configure the Claims Issuer to be Auth0
        options.ClaimsIssuer = "Auth0";

        options.Events = new OpenIdConnectEvents
        {
            // handle the logout redirection 
            OnRedirectToIdentityProviderForSignOut = (context) =>
            {
                var logoutUri = $"https://{Configuration["Auth0:Domain"]}/v2/logout?client_id={Configuration["Auth0:ClientId"]}";

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

                context.Response.Redirect(logoutUri);
                context.HandleResponse();

                return Task.CompletedTask;
            }
        };
    });

    services.ConfigureApplicationCookie(options => options.LoginPath = "/Index/Login");

    services.AddMvc();
}

有人知道如何在2.0中正确执行此操作吗?

您需要添加此代码段才能正常工作:

services.AddMvc()
    .AddRazorPagesOptions(options =>
    {
        options.Conventions.AuthorizeFolder("/");
        options.Conventions.AllowAnonymousToPage("/Account/Login");
    });

当我添加此代码时,我的代码开始工作并重定向到正确的登录页面。

您需要添加此代码段才能工作:

services.AddMvc()
    .AddRazorPagesOptions(options =>
    {
        options.Conventions.AuthorizeFolder("/");
        options.Conventions.AllowAnonymousToPage("/Account/Login");
    });

当我添加这个时,我的代码开始工作并重定向到正确的登录页面。

您找到解决方案了吗?我正在尝试将razor页面与Auth0一起使用,但无法解决此问题。@hs2d我没有。没有。我最终使用了Azure B2C,它在.NET核心web应用程序中运行得更好。@Rob,你有任何关于剃须刀页面和B2C的github示例吗?@Venky我没有-我最终使用了Azure B2C。所以在这一点上,我无法验证拉斐尔的答案是否正确。你找到解决这个问题的方法了吗?我正在尝试将razor页面与Auth0一起使用,但无法解决此问题。@hs2d我没有。没有。我最终使用了Azure B2C,它在.NET核心web应用程序中运行得更好。@Rob,你有任何关于剃须刀页面和B2C的github示例吗?@Venky我没有-我最终使用了Azure B2C。所以在这一点上,我无法验证拉斐尔的答案是否正确。