[英]Using Open ID Connect with Server Side Blazor
I'd like to use Open ID Connect with Identity Server 4 for authorization in my server side Blazor application. 我想在服务器端Blazor应用程序中使用Open ID Connect与Identity Server 4进行授权。 I've got the same setup working in a MVC application.
我在MVC应用程序中具有相同的设置。
With the newest .NET Core version, 3.0 Preview 6, it is possible to add the attribute ´@attribute [Authorize]´ to a site. 使用最新的.NET Core版本3.0 Preview 6,可以将“ @attribute [Authorize]”属性添加到站点。 But if I'm not authorized, I don't get redirected to the Identity Server to log in, as I am used from my MVC applications.
但是,如果没有获得授权,就不会重定向到Identity Server进行登录,因为我从MVC应用程序中使用了该服务器。 Instead the site only shows the message "Not authorized".
而是该站点仅显示消息“未授权”。
In Startup.cs I've got the following setup: 在Startup.cs中,我进行了以下设置:
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ClientId = "myClient";
options.SaveTokens = true;
});
and 和
app.UseAuthentication();
How do I tell the application, that I want to be redirected to the Identity Server if I'm not logged in? 如果我未登录,如何通知应用程序我要重定向到Identity Server?
EDIT: Codevisions answer works as a workaround. 编辑:Codevisions答案是一种解决方法。 I found pending github issues here and here , planned for .NET Core 3.0 Preview 7 that will possibly cover this issue officially.
我在这里和这里发现了尚未解决的github问题,计划在.NET Core 3.0 Preview 7中正式使用此问题。
Add to ConfigureServices
code below. 添加到下面的
ConfigureServices
代码。
services.AddMvcCore(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
The following code-snippet demonstrates how you can annotate a data controller in your Web API with the Authorize attribute to redirect an unauthenticated user to log in (perhaps Identity Server). 以下代码段演示了如何使用Authorize属性在Web API中注释数据控制器,以重定向未经身份验证的用户登录(也许是Identity Server)。
[Authorize(AuthenticationSchemes = OpenIdConnectDefaults.AuthenticationScheme)]
[Route("api/[controller]")]
public class SampleDataController : Controller
{
// ..
}
Note: Appropriate configuration is need in the Startup class 注意:在启动类中需要适当的配置
Note: If you use a Service instead of Web API, I'd advise you to discard the former and use the latter. 注意:如果您使用服务而不是Web API,建议您不要使用前者,而应使用后者。
Note: I was not aware of the @attribute directive, but why don't you give it a try like this and tell us if it works for you... 注意:我不知道@attribute指令,但是为什么不尝试一下并告诉我们它是否对您有用呢?
@attribute [Authorize(AuthenticationSchemes = OpenIdConnectDefaults.AuthenticationScheme)]
I'll try to add more info later on, but run your app and ask questions about issues you're facing... 稍后,我将尝试添加更多信息,但运行您的应用并询问有关您所面临问题的问题...
Hope this helps... 希望这可以帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.