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 – asp:repeater – 部分更改时的标头
- asp.net-mvc – MVC3如何在ViewModel中将多个复选框绑定到1
- 具有EntityDataSource的一个ASP.NET GridView中的两个相关数
- asp.net-mvc-4 – 如何通过MVC4 upshot上传相关实体
- asp.net-core – 找不到Swashbuckle.AspNetCore SwaggerOpe
- asp.net-mvc – 从视图重定向到另一个视图
- asp.net – 在同一站点上使用基本身份验证和表单身份验证
- 为什么每个人都在ASP.NET Webforms中依赖注入是困难的,当Pa
- asp.net-mvc – 从传递给局部视图的嵌套复杂对象获取值
- asp.net-mvc – ASP.Net MVC加载进度指标
- asp.net – 使用必填字段验证器更改文本框颜色 无
- asp.net – 如何在使用无cookie的静态内容服务器
- asp.net – HTTP保持活跃在一个大的Web应用程序
- asp.net-mvc-3 – ASP.NET MVC – 在不重写方法的
- asp.net-mvc – @ Html.ValidationSummary(true)
- 你如何正确使用UpdatePanel? (asp.net)
- 在ASP.NET中开发SharePoint Web部件
- asp.net-ajax – ASP.Net AJAX UpdatePanel无法触
- asp.net – EF是否在DbContext的不同实例之间缓存
- asp.net-mvc – ConfigurationManager读取错误的
