使用Open ID Connect与服务器端Blazor [英] Using Open ID Connect with Server Side Blazor

查看:100
本文介绍了使用Open ID Connect与服务器端Blazor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在服务器端Blazor应用程序中使用Open ID Connect与Identity Server 4进行授权.我已经在MVC应用程序中使用了相同的设置.

I'd like to use Open ID Connect with Identity Server 4 for authorization in my server side Blazor application. I've got the same setup working in a MVC application.

使用最新的.NET Core版本3.0 Preview 6,可以将属性"@attribute [Authorize]"添加到站点.但是,如果没有获得授权,就不会重定向到Identity Server进行登录,因为我从我的MVC应用程序中使用了该服务器.而是该网站仅显示消息未授权".

With the newest .NET Core version, 3.0 Preview 6, it is possible to add the attribute ´@attribute [Authorize]´ to a site. 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. Instead the site only shows the message "Not authorized".

在Startup.cs中,我进行了以下设置:

In Startup.cs I've got the following setup:

        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;
        });

        app.UseAuthentication();

如何告知应用程序,如果我没有登录,我想重定向到Identity Server?

How do I tell the application, that I want to be redirected to the Identity Server if I'm not logged in?

Codevisions的答案是一种解决方法.我在此处

Codevisions answer works as a workaround. I found pending github issues here and here, planned for .NET Core 3.0 Preview 7 that will possibly cover this issue officially.

推荐答案

添加到下面的ConfigureServices代码中.

services.AddMvcCore(options =>
{
    var policy = new AuthorizationPolicyBuilder()
        .RequireAuthenticatedUser()
        .Build();
    options.Filters.Add(new AuthorizeFilter(policy));
});

这篇关于使用Open ID Connect与服务器端Blazor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆