2017-09-18 45 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"); 
    }); 

當我加入這個,我的代碼開始工作,並重定向到正確的登錄頁面。