c# - ASP .NET Core 2.0 - JWT 外部认证
问题描述
我正在尝试开始在 ASP.NET Core 2.0 Web 应用程序上进行身份验证。
我的公司正在使用 Ping Federate,我正在尝试使用公司登录页面对我的用户进行身份验证,并使用我的签名密钥(X509SecurityKey
在此处)验证返回的令牌。
登录页面链接如下所示:
https://companyname.com/authorization.oauth2?response_type=code&redirect_uri=https%3a%2f%2fJWTAuthExample%2fAccount%2fLogin&client_id=CompanyName.Web.JWTAuthExample&scope=&state=<...state...>
开箱即用,我将 Startup.cs 配置为能够登录并挑战该站点。
我用 a 装饰了我的 HomeController,[Authorize(Policy="Mvc")]
但是当我访问其中一个页面时,我只得到一个空白页面。
当我添加它时,调试没有命中OnChallenge
orOnAuthenticationFailed
方法options.Events
(我认为因为用户需要首先进行身份验证)。
那么,为了重定向到我的身份验证网站,我缺少什么?它是内置的还是我必须进行一些手动配置?
(注意:在其他网络应用程序中,使用 asp net 框架,当身份验证失败时,我在 Authorize 属性中使用重定向)
相关文章:使用 .NET Core 2 的 AddJwtBearer 时,授权属性不会重定向到登录页面 - 从这篇文章来看,这是否意味着我没有使用正确的身份验证方法?我正在构建一个网络应用程序,而不是一个 API。
namespace JWTAuthExample
{
public class Startup
{
public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
{
Configuration = configuration;
HostingEnvironment = hostingEnvironment;
string certificatepath = Path.Combine(HostingEnvironment.ContentRootPath, $"App_Data\\key.cer");
KEY = new X509SecurityKey(new X509Certificate2(certificatepath));
}
public IConfiguration Configuration { get; }
public IHostingEnvironment HostingEnvironment { get; }
private string AUTH_LOGINPATH { get; } = Configuration["DefaultAuth:AuthorizationEndpoint"];
private X509SecurityKey KEY { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
{
options.IncludeErrorDetails = true;
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters
{
// Ensure token expiry
RequireExpirationTime = true,
ValidateLifetime = true,
// Ensure token audience matches site audience value
ValidateAudience = false,
ValidAudience = AUTH_LOGINPATH,
// Ensure token was issued by a trusted authorization server
ValidateIssuer = true,
ValidIssuer = AUTH_LOGINPATH,
// Specify key used by token
RequireSignedTokens = true,
IssuerSigningKey = KEY
};
});
services.AddAuthorization(options =>
{
options.AddPolicy("Mvc", policy =>
{
policy.AuthenticationSchemes.Add(JwtBearerDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
});
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
解决方案
按照布拉德的建议,
这是在 ASP NET 2.0 上执行 OpenId Connect 配置的代码示例
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.Authority = Configuration["AuthoritySite"];
options.ClientId = Configuration["ClientId"];
options.ClientSecret = Configuration["ClientSecret"];
options.Scope.Clear();
// options.Scope.Add("Any:Scope");
options.ResponseType = OpenIdConnectResponseType.CodeIdTokenToken;
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
options.TokenValidationParameters = new TokenValidationParameters
{
// Compensate server drift
ClockSkew = TimeSpan.FromHours(12),
// Ensure key
IssuerSigningKey = CERTIFICATE,
// Ensure expiry
RequireExpirationTime = true,
ValidateLifetime = true,
// Save token
SaveSigninToken = true
};
});
services.AddAuthorization(options =>
{
options.AddPolicy("Mvc", policy =>
{
policy.AuthenticationSchemes.Add(OpenIdConnectDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
});
});
}
更多详细信息:https ://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x?view=aspnetcore-2.1
推荐阅读
- google-cloud-platform - 如何将 DCM 标准报告展示次数与 Big Query 的拉取匹配?
- flutter - 小部件不会与其他小部件重叠
- sql-server - SSRS - 使用分页符/按元素分组修改 PageName
- google-apps-script - Google Apps 脚本 GET 和 POST 控制器不适用于 IoT 设备 (ESP32)
- reactjs - 如何使用 React 和 TypeScript 修改 reduxjs/toolkit 中的状态
- javascript - 为什么要按两下才能做我想做的事?反应原生
- python - 在 Python shell 中输入多行
- excel - XLOOKUP 匹配电子邮件
- javascript - 如何使用 videojs 禁用菜单项
- r - R:返回所有简单路径的边列表