Microsoft.AspNetCore.Authentication.JwtBearer或者Microsoft.AspNetCore.Authentication.OpenIdConnect
Microsoft.AspNetCore.Authentication的,现在需要安装 Microsoft.Extensions.Identity.Core
var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
var user = payload.RootElement;
Microsoft.AspNetCore.SpaServices.Extensions
// 跨域
app.UseCors(config =>
config.AllowAnyHeader()
.AllowAnyMethod()
.AllowAnyOrigin()
.AllowCredentials());
app.UseCors("default");
c.AddSecurityDefinition("Bearer", new ApiKeyScheme
{
Description = "JWT Authorization",
Name = "Authorization",
In = "header",
Type = "apiKey"
});
c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
{
{ "Bearer",new string[]{}}
});
var bearerScheme = new OpenApiSecurityScheme
{
Description = "JWT Authorization",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey
};
c.AddSecurityDefinition("Bearer", bearerScheme);
c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{ bearerScheme, new List<string>()}
});
c.AddSecurityDefinition("oauth2", new OAuth2Scheme
{
Type = "oauth2",
Flow = "authorizationCode",
TokenUrl = "/api/Account/GetToken",
AuthorizationUrl = oAuthConfiguration.Authority.EnsureEnd("/") + "connect/authorize",
Description = "OAuth2登陆授权",
Scopes = new Dictionary<string, string>
{
{ "api1", "测试"},
{ "openid","唯一标识" },
{ "profile","用户信息"}
}
});
c.AddSecurityRequirement(new Dictionary<string, Enumerable<string>>
{
{ "oauth2",new string[]{}}
});
var oauth2Scheme = new OpenApiSecurityScheme
{
Type = SecuritySchemeType.OAuth2,
Description = "OAuth2登陆授权",
Flows = new OpenApiOAuthFlows()
{
AuthorizationCode = new OpenApiOAuthFlow()
{
AuthorizationUrl = new Uri(oAuthConfiguration.Authority.EnsureEnd("/") + "connect/authorize"),
TokenUrl = new Uri("/api/Account/GetToken",UriKind.Relative),
Scopes = new Dictionary<string, string>
{
{"api1", "测试"},
{"openid", "唯一标识"},
{"profile", "用户信息"}
}
}
}
};
c.AddSecurityDefinition("oauth2", oauth2Scheme);
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{ oauth2Scheme, new List<string>(){"openid"}}
});
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/615/viewspace-2823894/ ,如需转载,请注明出处,否则将追究法律责任。