C#授权-如何在您的方法中使用索赔';s逻辑
我遵循了一些指南,设置了JWT身份验证,然后编写了一个授权策略 我有一个访问数据库的更新模块。您发送SQL参数,它将运行该查询。我的目标是只让用户更新他们自己的记录 为此,保险单将其电子邮件地址记录为Claim.email 我要做的是在更新模块中实现这一点 如何访问索赔。在更新模块中发送电子邮件,以便我可以执行一些基本逻辑?C#授权-如何在您的方法中使用索赔';s逻辑,c#,authorization,claims-based-identity,C#,Authorization,Claims Based Identity,我遵循了一些指南,设置了JWT身份验证,然后编写了一个授权策略 我有一个访问数据库的更新模块。您发送SQL参数,它将运行该查询。我的目标是只让用户更新他们自己的记录 为此,保险单将其电子邮件地址记录为Claim.email 我要做的是在更新模块中实现这一点 如何访问索赔。在更新模块中发送电子邮件,以便我可以执行一些基本逻辑? if( userEmail = Claim.Email){ update database } else { return error } startup.cs中的
if( userEmail = Claim.Email){
update database
}
else {
return error
}
startup.cs中的我的授权策略:
// Authorization Policy
// Should stop users from being able to update other user's details once authenticated.
services.AddAuthorization(x =>
{
var policy = new AuthorizationPolicyBuilder();
var emailAuthPolicy = policy
.RequireAuthenticatedUser() // Make sure user is authenticated before authorizing them
.RequireClaim(ClaimTypes.Email) // Require email address as a claim
.RequireRole("normal", "super", "admin")
.Build();
x.DefaultPolicy = emailAuthPolicy;
// A custom policy for Claim.Email
x.AddPolicy("Claim.Email", policyBuilder =>
{
policyBuilder.AddRequirements(new emailAuthRequirement(ClaimTypes.Email));
});
});
自定义策略处理程序:
// Policy Claims based Authentication - We need to write a Requirement, A Handler and a Policy
// Requirement
public class emailAuthRequirement : IAuthorizationRequirement
{
// Constructor
public emailAuthRequirement(string email)
{
// The requirement list to check.
ClaimType = email;
}
public string ClaimType { get; set; }
}
// Handler
// The handler checks if the user passes/fails the listed checks in the Requirement
public class emailAuthHandler : AuthorizationHandler<emailAuthRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, emailAuthRequirement requirement)
{
var claimCheck = context.User.Claims.Any(x => x.Type == requirement.ClaimType);
// If the user passes the claim check give them a Succeed value.
if (claimCheck)
{
context.Succeed(requirement);
}
return Task.CompletedTask;
}
}
//基于策略声明的身份验证-我们需要编写需求、处理程序和策略
//要求
公共类emailAuthRequirement:IAAuthorizationRequirement
{
//建造师
公共电子邮件授权要求(字符串电子邮件)
{
//需要检查的需求列表。
ClaimType=电子邮件;
}
公共字符串ClaimType{get;set;}
}
//处理者
//处理程序检查用户是否通过/未通过需求中列出的检查
公共类emailAuthHandler:AuthorizationHandler
{
受保护的覆盖任务HandleRequirementAsync(授权HandlerContext上下文,emailAuthRequirement要求)
{
var claimCheck=context.User.Claims.Any(x=>x.Type==requirement.ClaimType);
//如果用户通过索赔检查,则给他们一个SUCCESS值。
如果(索赔检查)
{
成功(要求);
}
返回Task.CompletedTask;
}
}
最后,我在我想使用逻辑的模块上使用授权标记:
// UPDATE USER DETAILS
[Authorize(Roles = "normal, super, admin")]
[Authorize(Policy = "Claim.Email")]
[HttpPost("update")]
public async Task<IActionResult> Update(int id, string email, string firstname, string surname)
{
// Need to check if the email address passed into this module is the same as in the claim ?
if( [claim logic ?] ){
// Make the change in the database
IUsers user = new Users();
bool update = await Task.Run(() => user.UpdateUser(id, email, firstname, surname));
}
else
{
// Failed claim logic check
return BadRequest();
}
}
//更新用户详细信息
[授权(角色=“普通、超级、管理员”)]
[授权(Policy=“Claim.Email”)]
[HttpPost(“更新”)]
公共异步任务更新(整数id、字符串电子邮件、字符串名字、字符串姓氏)
{
//需要检查传入此模块的电子邮件地址是否与索赔中的相同?
如果([索赔逻辑?]){
//在数据库中进行更改
IUsers user=新用户();
bool update=wait Task.Run(()=>user.UpdateUser(id,email,firstname,姓氏));
}
其他的
{
//索赔逻辑检查失败
返回请求();
}
}
我正在阅读有关identity server和identity的文章-您就是这样做的吗?创建标识,然后访问代码中的标识
谢谢你的帮助。我想使用ASP.NET内置的身份框架会是一个更容易的选择。我所遵循的大多数指南都试图整合他们自己的用户管理系统。Identity framework做了我需要的事情,而且是开箱即用的。