猿问

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 配置为能够登录并挑战此站点。

我用一个装饰我的 HomeController[Authorize(Policy="Mvc")]但是当我访问其中一个页面时,我只得到一个空白页面。

当我将它添加到Debug 时,它没有命中OnChallengeOnAuthenticationFailed方法options.Events(我认为是因为用户需要先进行身份验证)。

那么,为了重定向到我的身份验证网站,我缺少什么?它是内置的还是我必须进行一些手动配置?

(注意:在其他 web 应用程序中,使用 asp net 框架,我在身份验证失败时在 Authorize 属性中使用重定向)


狐的传说
浏览 228回答 1
1回答

catspeake

按照布拉德的建议,这是在 ASP NET 2.0 上执行 OpenId Connect 配置的代码示例public void ConfigureServices(IServiceCollection services){&nbsp; &nbsp; services.AddMvc();&nbsp; &nbsp; services.AddAuthentication(options =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;&nbsp; &nbsp; &nbsp; &nbsp; options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;&nbsp; &nbsp; &nbsp; &nbsp; options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;&nbsp; &nbsp; })&nbsp; &nbsp; .AddCookie()&nbsp; &nbsp; .AddOpenIdConnect(options =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; options.Authority = Configuration["AuthoritySite"];&nbsp; &nbsp; &nbsp; &nbsp; options.ClientId = Configuration["ClientId"];&nbsp; &nbsp; &nbsp; &nbsp; options.ClientSecret = Configuration["ClientSecret"];&nbsp; &nbsp; &nbsp; &nbsp; options.Scope.Clear();&nbsp; &nbsp; &nbsp; &nbsp; // options.Scope.Add("Any:Scope");&nbsp; &nbsp; &nbsp; &nbsp; options.ResponseType = OpenIdConnectResponseType.CodeIdTokenToken;&nbsp; &nbsp; &nbsp; &nbsp; options.SaveTokens = true;&nbsp; &nbsp; &nbsp; &nbsp; options.GetClaimsFromUserInfoEndpoint = true;&nbsp; &nbsp; &nbsp; &nbsp; options.TokenValidationParameters = new TokenValidationParameters&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Compensate server drift&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ClockSkew = TimeSpan.FromHours(12),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Ensure key&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IssuerSigningKey = CERTIFICATE,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Ensure expiry&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RequireExpirationTime = true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ValidateLifetime = true,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Save token&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SaveSigninToken = true&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; });&nbsp; &nbsp; services.AddAuthorization(options =>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; options.AddPolicy("Mvc", policy =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; policy.AuthenticationSchemes.Add(OpenIdConnectDefaults.AuthenticationScheme);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; policy.RequireAuthenticatedUser();&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });}更多详细信息:https : //docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/identity-2x?view=aspnetcore-2.1
随时随地看视频慕课网APP
我要回答