【发布时间】:2020-04-14 23:44:18
【问题描述】:
我目前正在通过这篇文章将 okta 集成到 Blazor 服务器端应用程序中
https://developer.okta.com/blog/2019/10/16/csharp-blazor-authentication
我目前收到“抱歉,这个地址没有任何东西”。
我希望有人可以为我的问题提供建议。
或者有人知道将 okta 集成到 Blazor 服务器端应用程序中的示例吗?
请告诉我。
任何帮助将不胜感激。我完全在旋转我的轮子。
这是我的 okta 常规设置:
以下是我的解决方案中的代码(我多次查看该帖子以确保它是正确的。但我想我可能错过了一些东西。)
启动配置服务
services.AddAuthorizationCore();
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.ClientId = Configuration["Okta:ClientId"];
options.ClientSecret = Configuration["Okta:ClientSecret"];
options.Authority = Configuration["Okta:Issuer"];
options.CallbackPath = "/authorization-code/callback";
options.ResponseType = "code";
options.SaveTokens = true;
options.UseTokenLifetime = false;
options.GetClaimsFromUserInfoEndpoint = true;
options.Scope.Add("openid");
options.Scope.Add("profile");
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name"
};
});
}
启动配置
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// 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.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
MainLayout.razor
<div class="top-row px-4">
<AuthorizeView>
<Authorized>
<a href="Identity/Account/Manage">Hello, @context.User.Identity.Name!</a>
<a href="Identity/Account/LogOut">Log out</a>
</Authorized>
<NotAuthorized>
<a href="Identity/Account/Register">Register</a>
<a href="Identity/Account/Login">Log in</a>
</NotAuthorized>
</AuthorizeView>
<a href="https://docs.microsoft.com/en-us/aspnet/" target="_blank">About</a>
</div>
App.razor
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState> ```
【问题讨论】:
-
你可以这样做吗:在 App 组件的顶部添加:@inject NavigationManager NavigationManager 在
元素中放置:@{ var returnUrl = NavigationManager.ToBaseRelativePath(NavigationManager.Uri ); Console.WriteLine(returnUrl); } 请报告打印的值 -
我得到的值是:"Identity/Account/Login"
-
从这个选项开始。CallbackPath = "/authorization-code/callback";您已在此处发布了文件的图像。这个文件在世界上的什么地方?无论如何,看看你是否可以并且应该改变与那里相关的回调。它应该提供现有的 url。祝你好运,并报告
-
好的,我会给它,这可能需要一点时间,但我会告诉你的。谢谢。
标签: blazor okta blazor-server-side