首页 文章

从单个'site'运行IdentityServer4和API以及Web应用程序

提问于
浏览
0

我们有一个用Core MVC 1编写的遗留系统,使用IdentityServer4进行API访问,使用Identity进行用户管理 . 该站点还包括一组API控制器,移动应用程序连接到这些控制器以进行身份验证和数据 .

我们一直存在一般稳定性问题,而这些问题我们无法深究 . 我们已决定将系统升级到最新版本的MVC Core,并且在此过程中IdentityServer4需要升级 .

问题是认证管道在版本(核心MVC 1-2和身份1-2)之间发生了巨大变化,我们无法确定有效的配置 .

总之,我们需要:

  • 用于网站访问的Cookie身份验证
    用于应用访问的
  • OAuth 2密码授予流程

但是,尽管这个设置适用于旧版本,但它似乎不想在较新的设置上播放 . 看来我们可以有一个或另一个,但不是两个 . 似乎没有任何示例项目可用于演示此类设置 .

我理解这种设置并不理想,因为这些系统应该分开,我将提出这样的建议 . 我已经看到通过使用 MapFrom 进行承载认证的管道设置来路由api请求的提示,但是还没有设法确定工作设置 .

更新:Startup.cs

services.AddIdentity<ApplicationUser, IdentityRole>(o =>
        {
            o.Password.RequireDigit = false;
            o.Password.RequireLowercase = false;
            o.Password.RequireUppercase = false;
            o.Password.RequireNonAlphanumeric = false;
            o.Password.RequiredLength = 6;
        })
        .AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();

var AuthServerConfig = new IdentityServerConfig(Configuration.GetSection("IdentityServer"));
            var IdentityCert = AuthServerConfig.GetCerttificate();
            var IdentityConfig = services.AddIdentityServer()
                                         .AddInMemoryIdentityResources(AuthServerConfig.GetIdentityResources())
                                         .AddInMemoryApiResources(AuthServerConfig.GetApiResources())
                                         .AddInMemoryClients(AuthServerConfig.GetClients())
                                         .AddAspNetIdentity<ApplicationUser>();

//to secure the API
services.AddAuthentication("Bearer")
    .AddIdentityServerAuthentication(options =>
     {
           options.Authority = AuthServerConfig.Settings.RootUrl;
           options.RequireHttpsMetadata = false;
           options.ApiName = AuthServerConfig.Settings.Scope;
      });

在配置方法中,我们有:

app.UseIdentityServer();
app.UseAuthentication();

我们现在的阶段是IdentityServer似乎可以运行,因为可以请求令牌 . 只要该 endpoints 具有以下属性,您就可以调用API endpoints 并获得访问权限:

[Authorize(AuthenticationSchemes = "Bearer")]

但是,我们希望使用Identity Cookies和Bearer令牌对API进行身份验证,因为在登录时有一个用于查询API的虚张声势的UI .

仅使用 [Authorize] 属性将允许通过cookie访问它,但不能通过Postman访问令牌(401)

1 回答

  • 0

    不确定这是否已经解决,但我个人所做的是通过cookie和承载令牌对API进行身份验证,实现两种方案:

    例如

    services.AddAuthentication()
            .AddCookie(options => {
                options.LoginPath = "/Account/Unauthorized/";
                options.AccessDeniedPath = "/Account/Forbidden/";
            })
            .AddJwtBearer(options => {
                options.Audience = "http://localhost:5001/";
                options.Authority = "http://localhost:5000/";
            });
    

    在api控制器中我使用 [Authorize(AuthenticationSchemes = AuthSchemes)]

    [Authorize(AuthenticationSchemes = AuthSchemes)]
    public class TodoController: Controller
    

    有关它们的不同方案和用法的更多详细信息,请访问:

    https://docs.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme?tabs=aspnetcore2x

    希望有所帮助 .

相关问题