I have tried to implement Okta login in my app. I'm running a test app to figure it out before I edit the real app. I load the url and it authenticates through Okta as it should. Then it does to a page saying "Loading..." for a few seconds and then gives an error:
本地重定向(RedirectUri)应该重定向到"/".相反,它再次循环回到Login函数.
加入时间:清华2007年01月25日下午3:33
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
namespace TestOktaLogin.Controllers {
public class LoginController : Controller {
[HttpGet("Login")]
public IActionResult Login([FromQuery] string returnUrl) {
var redirectUri = returnUrl is null ? "/" : "/" + returnUrl;
if (User.Identity.IsAuthenticated) {
return LocalRedirect(redirectUri); //It does not return. It just reloads the function.
}
return Challenge();
}
// This is the method the Logout button should get to when clicked.
[HttpGet("Logout")]
public async Task<ActionResult> Logout([FromQuery] string returnUrl) {
var redirectUri = returnUrl is null ? Url.Content("~/") : "/" + returnUrl;
if (!User.Identity.IsAuthenticated) {
return LocalRedirect(redirectUri);
}
await HttpContext.SignOutAsync();
return LocalRedirect(redirectUri);
}
}
}
重定向到登录.
using Microsoft.AspNetCore.Components;
namespace TestOktaLogin.Components {
public partial class RedirectToLogin : ComponentBase {
[Inject] public NavigationManager Navigation { get; set; }
protected override async Task OnInitializedAsync() {
var returnUrl = Navigation.ToBaseRelativePath(Navigation.Uri);
Navigation.NavigateTo($"Login?returnUrl={returnUrl}", true);
}
}
}```
家.razor, which is the default to "/":
@page"/"
@属性[授权]
家
Hello, world!
欢迎使用您的新apply.`
Program.cs:
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using TestOktaLogin.Components;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.AddMvc();
builder.Services.AddAuthentication(authOptions => {
authOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
authOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
authOptions.DefaultSignOutScheme = CookieAuthenticationDefaults.AuthenticationScheme;
authOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
}).AddOpenIdConnect(oidcOptions => {
oidcOptions.ClientId = builder.Configuration["Okta:ClientId"];
oidcOptions.ClientSecret = builder.Configuration["Okta:ClientSecret"];
oidcOptions.CallbackPath = "/authorization-code/callback";
oidcOptions.Authority = builder.Configuration["Okta:Issuer"];
oidcOptions.ResponseType = "code";
oidcOptions.SaveTokens = true;
oidcOptions.Scope.Add("openid");
oidcOptions.Scope.Add("profile");
oidcOptions.TokenValidationParameters.ValidateIssuer = false;
oidcOptions.TokenValidationParameters.NameClaimType = "name";
}).AddCookie();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) {
app.UseExceptionHandler("/Error", createScopeForErrors: true);
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseAntiforgery();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
app.Run();
编辑: 这是我的路由.剃须刀
@using TestOktaLogin.Components.Layout
<CascadingValue Name="AccessToken" Value="AccessToken">
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData=@routeData DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<RedirectToLogin /> @* Would this cause the problem? *@
</NotAuthorized>
<Authorizing>
Authorizing...
</Authorizing>
</AuthorizeRouteView>
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
</CascadingValue>
@code{
[Parameter] public string AccessToken { get; set; }
}
这是MainLayout.razor
@inherits LayoutComponentBase
<div>
<LoginDisplay/>
</div>
@Body
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">🗙</a>
</div>
如果有任何额外的代码我需要上传,让我知道.谢谢你的帮助!