asp.net-core – 使用AspNet Core 2.0进行Google JWT身份验证
我想在我的ASP.NET Core 2.0 web api中集成google身份验证,我无法弄清楚如何让它工作.
我在我的Startup.cs ConfigureServices中有这个代码: services.AddIdentity<ApplicationUser,IdentityRole>() .AddDefaultTokenProviders(); services.AddAuthentication() .AddGoogle(googleOptions => { googleOptions.ClientId = Configuration["Authentication:Google:ClientId"]; googleOptions.ClientSecret = Configuration["Authentication:Google:ClientSecret"]; }); 这在Configure(IApplicationBuilder app,IHostingEnvironment env)中: app.UseAuthentication(); 当我导航到授权端点时,结果是302 Found,因为可能是它重定向到某个登录端点(我从未创建过).如何阻止重定向,只是让API期望一个令牌,如果没有提供令牌,则返回401? 解决方法
为后代发布我的终极方法.
正如Tratcher所指出的,AddGoogle中间件实际上并不适用于JWT身份验证流程.经过更多的研究,我意识到我最终想要的是这里描述的内容: 所以我的下一个问题是 >我不能再依赖标准的dotnet核心Jwt auth中间件,因为我需要将谷歌令牌验证委托给谷歌图书馆 经过更多挖掘,我发现使用此类和方法将JWT验证支持添加到C#here: 接下来我需要弄清楚如何替换内置的JWT验证.从这个问题我想出了一个方法: 这是我的自定义GoogleTokenValidator: public class GoogleTokenValidator : ISecurityTokenValidator { private readonly JwtSecurityTokenHandler _tokenHandler; public GoogleTokenValidator() { _tokenHandler = new JwtSecurityTokenHandler(); } public bool CanValidateToken => true; public int MaximumTokenSizeInBytes { get; set; } = TokenValidationParameters.DefaultMaximumTokenSizeInBytes; public bool CanReadToken(string securityToken) { return _tokenHandler.CanReadToken(securityToken); } public ClaimsPrincipal ValidateToken(string securityToken,TokenValidationParameters validationParameters,out SecurityToken validatedToken) { validatedToken = null; var payload = GoogleJsonWebSignature.ValidateAsync(securityToken,new GoogleJsonWebSignature.ValidationSettings()).Result; // here is where I delegate to Google to validate var claims = new List<Claim> { new Claim(ClaimTypes.NameIdentifier,payload.Name),new Claim(ClaimTypes.Name,new Claim(JwtRegisteredClaimNames.FamilyName,payload.FamilyName),new Claim(JwtRegisteredClaimNames.GivenName,payload.GivenName),new Claim(JwtRegisteredClaimNames.Email,payload.Email),new Claim(JwtRegisteredClaimNames.Sub,payload.Subject),new Claim(JwtRegisteredClaimNames.Iss,payload.Issuer),}; try { var principle = new ClaimsPrincipal(); principle.AddIdentity(new ClaimsIdentity(claims,AuthenticationTypes.Password)); return principle; } catch (Exception e) { Console.WriteLine(e); throw; } } } 在Startup.cs中,我还需要清除默认的JWT验证,并添加我的自定义验证: services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(o => { o.SecurityTokenValidators.Clear(); o.SecurityTokenValidators.Add(new GoogleTokenValidator()); } 也许有一个更简单的方法,但这是我降落的地方,它似乎工作正常!我做了额外的工作,为了简单起见,我离开了这里,例如,检查我的用户的数据库中是否已有用户与谷歌提供的声明匹配,所以如果上面的代码不是100%工作,我很抱歉我可能无意中删除了一些东西. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – “填充无效,不能删除”异常WebResource.axd
- asp.net-mvc – 如何在MVC4中调用自定义模型绑定器?
- asp.net – 手动更新表单认证券:
- asp.net-mvc-3 – 在MVC3中禁用HTML.TextAreaFor
- asp.net – 在现有系统上重构可测试性
- asp.net-mvc – 使用MVC的Unity 2.0 Web.config设置
- ASP.NET 服务器部署IIS程序池进程标示配置要和SQL SERVER一
- 在对ASP.NET MVC Action的AJAX请求期间有网络请求超时时会发
- 如何返回404状态,无效参数传递给我的ASP.NET MVC控制器?
- asp-classic – asp CDO.Message.1错误’80040213’传输无法
- asp.net-mvc – MVC3将@model传递给局部视图
- asp.net-mvc – Razor视图没有布局
- asp.net-mvc – 我可以在同一AppHarbor网站上运行
- asp.net – 为什么<%=%>在一种情况下工作,但在
- asp.net-mvc-2 – 仅在运行调试器时出现Antiforg
- asp.net – 在IIS 7或IIS 7.5集成模式下,默认文档
- 反射及LINQ结果集转换dataTable并序列化 将Da
- asp.net – 设置.net web-application的默认页面
- asp.net – JSON:序列化从IEnumerable派生的类型
- asp.net-mvc – 组合ASP.NET MVC Web应用程序的最