使用基于角色的授权IS4

时间:2020-05-07 17:23:26

标签: asp.net-core authorization asp.net-identity single-sign-on identityserver4

这是一个多方面的问题,主要是因为我是IS4的新手,并且通常使用SSO作为授权手段。

我的目标是允许我的用户根据存储在IS4服务器上的角色来查看MVC应用程序的不同部分。

我正在使用快速入门IdentityServerAspNetIdentity,它已连接到我的AspNetIdentity数据库。

这是IS4 ConfigureServices:

services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(Configuration.GetConnectionString("IdentityContext")));

services.AddIdentity<ApplicationUser, IdentityRole>()
    .AddEntityFrameworkStores<ApplicationDbContext>()
    .AddDefaultTokenProviders();

services.AddIdentityServer(options =>
    {
        options.Events.RaiseErrorEvents = true;
        options.Events.RaiseInformationEvents = true;
        options.Events.RaiseFailureEvents = true;
        options.Events.RaiseSuccessEvents = true;
    })
    .AddConfigurationStore<ConfigurationDbContext>(configDb =>
    {
        configDb.ConfigureDbContext = db => db.UseNpgsql(Configuration.GetConnectionString("QuickstartContext"),
            sql => sql.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name));
    })
    .AddOperationalStore<PersistedGrantDbContext>(operationDb =>
    {
        operationDb.ConfigureDbContext = db => db.UseNpgsql(Configuration.GetConnectionString("QuickstartContext"),
            sql => sql.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name));
    })
    .AddAspNetIdentity<ApplicationUser>()
    .AddProfileService<CustomClaimService>()
    .AddDeveloperSigningCredential();       // not recommended for production - you need to store your key material somewhere secure       

services.AddTransient<IProfileService, CustomClaimService>();

在我的MVC客户端上:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();

    JwtSecurityTokenHandler.DefaultMapInboundClaims = false;

    services.AddAuthentication(options =>
    {
        options.DefaultScheme = "Cookies";
        options.DefaultChallengeScheme = "oidc";
    })
        .AddCookie("Cookies")
        .AddOpenIdConnect("oidc", options =>
        {
            options.Authority = "https://localhost:5000";
            options.RequireHttpsMetadata = false;

            options.ClientId = "mvc";
            options.ClientSecret = "49C1A7E1-0C79-4A89-A3D6-A37998FB86B0";
            options.ResponseType = "code";
            options.SaveTokens = true;
            options.GetClaimsFromUserInfoEndpoint = true;
            options.Scope.Add("api1");
            options.Scope.Add("offline_access");
            options.Scope.Add("profile");
            options.Scope.Add("test");
            options.CallbackPath = "/Home/SignIn";

        });

    services.AddAuthorization(options =>
    {
        options.AddPolicy("admin", policyAdmin =>
        {
            policyAdmin.RequireClaim("role", "admin");
        });
    });
}

我的用户可以通过IS4罚款登录,并且我可以在索引页面上看到声明

@using Microsoft.AspNetCore.Authentication

<h2>Claims</h2>

<dl>
    @foreach (var claim in User.Claims)
    {
        <dt>@claim.Type</dt>
        <dd>@claim.Value</dd>
    }
</dl>

<h2>Properties</h2>

<dl>
    @foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)
    {
        <dt>@prop.Key</dt>
        <dd>@prop.Value</dd>
    }
</dl>

但是,该视图中缺少一些用户声明。我在创建的作用域“测试”中添加了“角色”声明。我的用户在AspNetUserClaims中有此声明,如果我在IS4服务器中调试,则会在Context.Subject下看到该声明。

但是一旦登录并重定向回我的MVC应用程序,该声明不再存在。我不知道为什么这一要求没有得到解决并且陷入僵局。

任何帮助将不胜感激,此外,还有更好的方法将AspNetUserRoles与IS4集成吗?

0 个答案:

没有答案
相关问题