2017-09-18 70 views
0

我创建了一个Razor Pages应用,Auth0作为身份验证提供程序,我正在运行LoginPath问题。我见过的其他StackOverflow的答案是说你应该把这个入ConfigureServices方法:Auth0和Asp.Net Core 2.0 Razor页面登录路径问题

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

我试图把,下面的代码services.AddAuthentication部分,但这并不重定向到/首页/登录。我没有看到其他地方如何正确获取[Authorize]属性失败重定向到Auth0登录页面。我想如果我能得到的路径设置为索引页的代码可以运行:

public async void OnGetLogin(string returnUrl = "/") 
{ 
    await HttpContext.ChallengeAsync("Auth0", new AuthenticationProperties() { RedirectUri = returnUrl }); 
} 

我的全ConfigureServices代码:

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做到这一点?

+0

您是否找到解决方案?我正在尝试使用Auth0一起使用剃须刀页面,并且无法弄清楚。 – hs2d

+0

@ hs2d我没有。没有。我最终使用Azure B2C,并且它在.NET Core Web应用程序中运行得更好。 – Rob

回答

0

你需要为了加入这个片段的工作:

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

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