Asp.net core mvc 为Azure用户.net核心添加基于本地数据库的自定义授权声明
我正在尝试识别具有Azure AD电子邮件地址的数据库用户,然后根据本地数据库用户的属性向Azure AD身份验证用户添加自定义声明。在startup.cs中,我得到:Asp.net core mvc 为Azure用户.net核心添加基于本地数据库的自定义授权声明,asp.net-core-mvc,azure-active-directory,claims-based-identity,asp.net-authorization,Asp.net Core Mvc,Azure Active Directory,Claims Based Identity,Asp.net Authorization,我正在尝试识别具有Azure AD电子邮件地址的数据库用户,然后根据本地数据库用户的属性向Azure AD身份验证用户添加自定义声明。在startup.cs中,我得到: public void ConfigureServices(IServiceCollection services) { ... services.AddScoped<IClaimsTransformer, ClaimsTransformer>(); ... } public void Co
public void ConfigureServices(IServiceCollection services)
{
...
services.AddScoped<IClaimsTransformer, ClaimsTransformer>();
...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, KayttajatContext context)
{
...
app.UseClaimsTransformation(async (c) =>
{
IClaimsTransformer transformer = c.Context.RequestServices.GetRequiredService<IClaimsTransformer>();
return await transformer.TransformAsync(c);
});
...
}
public void配置服务(IServiceCollection服务)
{
...
services.addScope();
...
}
public void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂、KaytaJatContext上下文)
{
...
应用程序使用请求传输格式(异步(c)=>
{
IClaimsTransformer=c.Context.RequestServices.GetRequiredService();
返回等待变压器。TransformAsync(c);
});
...
}
然后ClaimsTransformer.cs看起来像这样:
namespace Authtest
{
public class ClaimsTransformer : IClaimsTransformer
{
private readonly KayttajatContext _context;
public ClaimsTransformer(KayttajatContext dbContext)
{
_context = dbContext;
}
public async Task<ClaimsPrincipal> TransformAsync(ClaimsTransformationContext ctrans)
{
string sposti = ((ClaimsIdentity)ctrans.Principal.Identity).Name;
var user = await _context.Henkilöt.FirstOrDefaultAsync(t => t.Sposti == sposti);
if (user.Sposti == sposti)
{
((ClaimsIdentity)ctrans.Principal.Identity).AddClaim(new Claim(ClaimTypes.Role, "Administrator"));
((ClaimsIdentity)ctrans.Principal.Identity).AddClaim(new Claim(ClaimTypes.Role, "User"));
}
else
{
((ClaimsIdentity)ctrans.Principal.Identity).AddClaim(new Claim(ClaimTypes.Role, "User"));
}
return ctrans.Principal;
}
}
}
命名空间验证测试
{
公共类索赔转换格式:ICLAIMSSTransformer
{
私有只读KaytaJatContext_上下文;
公共索赔转换人(KaytajatContext数据库上下文)
{
_context=dbContext;
}
公共异步任务TransformAsync(ClaimTransformationContext ctrans)
{
字符串sposti=((ClaimsIdentity)ctrans.Principal.Identity).Name;
var user=wait_context.Henkilöt.FirstOrDefaultAsync(t=>t.Sposti==Sposti);
if(user.Sposti==Sposti)
{
((ClaimsIdentity)ctrans.Principal.Identity).AddClaim(新索赔(ClaimTypes.Role,“管理员”);
((ClaimsIdentity)ctrans.Principal.Identity).AddClaim(新Claim(ClaimTypes.Role,“User”);
}
其他的
{
((ClaimsIdentity)ctrans.Principal.Identity).AddClaim(新Claim(ClaimTypes.Role,“User”);
}
返回ctrans.Principal;
}
}
}
但这给了我“NullReferenceException:对象引用未设置为对象的实例”。在if(user.Sposti==Sposti)
若我改为将字符串值赋给其中一个变量,If语句就可以正常工作。我不知道我做错了什么?它与异步有关吗?请帮助我,这让我发疯。我试图调用string
sposti=((ClaimsIdentity)ctrans.Principal.Identity)代码>在设置之前。现在可以工作了,谢谢:)看起来用户对象为空。可能是因为异步first或default导致我们无法及时返回。是的,我也怀疑这一点,我如何确保它提供了一个值?我在异步编程方面是个差劲的家伙。它将完成盲射。但是你需要首先确保数据库里有那封邮件。一旦确认了这一点,您就需要担心异步了。我100%确信它确实存在,我可以这样做,例如:`if(user.Sposti=='user@emailadress.com)而且还可以。