【发布时间】:2021-09-18 02:59:53
【问题描述】:
我有两个应用程序,Blazor 和 IdentityServer。我注意到在ConfigureServices method options.ClaimActions.MapUniqueJsonKey("role","role") 下的Startup.cs 文件和使用@context.user.identity.name 的index.razor 文件内部它返回null。但是当我评论 claimactions 时,将其替换为以下内容:
options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name"
};
options.UseTokenLifetime = false;
它将返回当前用户的电子邮件。 当我用上面的代码替换 claimactions.mapuniquejsonkey 行时它只返回一个值有什么原因吗?我试图理解为什么会这样。我在有关 TokenValidationParameters 的文档中看到了,但仍然没有像我想的那样理解它。
当我有 options.ClaimActions.MapUniqueJsonKey("role","role") 行时,它返回 null:
当我注释掉该行并将其替换为上面的这两行时:
如果您想查看完整代码,请参阅以下内容:
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton(sp => new HttpClient { BaseAddress = new Uri("http://localhost:36626") }); // WebApi project
services.AddTransient<IWeatherForecastServices, WeatherForecastServices>();
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.SignOutScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.Authority = "https://localhost:5443"; // IdentityServer Project
options.ClientId = "interactive";
options.ClientSecret = "KEY";
options.ResponseType = "code";
options.Scope.Add("profile"); // default scope
options.Scope.Add("scope2");
options.Scope.Add("roles");
options.Scope.Add("permissions");
options.Scope.Add("email");
options.ClaimActions.MapUniqueJsonKey("role", "role");
/* options.TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name"
};
options.UseTokenLifetime = false; */
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
});
services.AddScoped<TokenProvider>();
services.AddCors(options =>
{
options.AddPolicy("Open", builder => builder.AllowAnyOrigin().AllowAnyHeader());
}
);
services.AddAuthorization(options =>
{
options.AddPolicy(Policy.Policies.IsUser,
Policy.Policies.IsUserPolicy());
});
}
// 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.UseCors("Open");
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
Index.razor:
Index.razor
<AuthorizeView Policy="@Policy.Policies.IsUser">
<h3>Welcome, <b>@context.User.Identity.Name</b></h3>
<p>You can only see this if you satisfy the IsUser policy.</p>
</AuthorizeView>
来自 IdentityServer 的Config.cs:
.......
new Client
{
ClientId = "interactive",
ClientSecrets = { new Secret("KEY".Sha256()) },
RequirePkce = true,
AllowedGrantTypes = GrantTypes.Code,
RedirectUris = { "https://localhost:5445/signin-oidc", "https://localhost:44327/signin-oidc" },
FrontChannelLogoutUri = "https://localhost:5445/signout-oidc",
PostLogoutRedirectUris = { "https://localhost:5445/signout-callback-oidc" },
AlwaysIncludeUserClaimsInIdToken = true,
AllowOfflineAccess = true,
AllowedScopes = { "openid", "profile", "email" , "scope2" ,"weatherforecast-api","roles","permissions"}
},
};
【问题讨论】:
标签: c# asp.net-identity blazor identityserver4 blazor-server-side