C# 如何将新添加的声明持久化到Asp.NETCore3.1中现有的HttpContext用户
我是.Net Core新手,在Startup.cs文件中配置了如下身份验证-C# 如何将新添加的声明持久化到Asp.NETCore3.1中现有的HttpContext用户,c#,asp.net-core,model-view-controller,C#,Asp.net Core,Model View Controller,我是.Net Core新手,在Startup.cs文件中配置了如下身份验证- public void ConfigureServices(IServiceCollection services) { // To be able to access HttpContext services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
public void ConfigureServices(IServiceCollection services)
{
// To be able to access HttpContext
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(o => o.LoginPath = "/login");
// Most of the code removed for brevity
}
如何使这些新添加的声明在进一步的请求中持久化?只有在再次调用
SignInAsync
时,才会在ClaimsPrinciple/User的持久化中添加声明。因此,将找到您的第一个声明,因为这些声明是在用户登录之前添加的。但是,当您添加更多声明时,它们不会被“保存”,因为用户的身份尚未重置
如果您要添加更多索赔,在首次登录之前,这应该可以:
var authProperties=新的AuthenticationProperties
{
IssuedUtc=DateTimeOffset.UtcNow,
ExpiresUtc=DateTimeOffset.UtcNow.AddHours(1),
ispersist=false,
};
等待httpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
_httpContextAccessor.HttpContext.User,
版权所有),;
我读了那篇文章
要创建包含用户信息的cookie,请构造
ClaimsPrincipal
。用户信息被序列化并存储在
饼干SignInAsync
创建加密cookie并将其添加到
目前的反应。如果未指定AuthenticationScheme
,则
使用默认方案
因此,您不能将声明添加到已创建的ClaimsPrincipal
,因为声明已存储在cookie中
对我有效的解决方案是创建新的ClaimsPrincipal
并创建新的cookie作为-
public async Task AddPortalToCurrentUserClaimsAsync(Guid companyUid, Guid userUid)
{
var portal = await _unitOfWork.Portals.All().FirstOrDefaultAsync(p => p.CompanyUid == companyUid && p.UserUid == userUid).ConfigureAwait(false);
if (portal == null) return;
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, userUid.ToString()),
new Claim(CustomClaimTypes.CompanyGuid, companyUid.ToString()),
new Claim(CustomClaimTypes.PortalId, portal.Id.ToString()),
new Claim(CustomClaimTypes.PortalName, portal.Name)
};
var authProperties = new AuthenticationProperties
{
IssuedUtc = DateTimeOffset.UtcNow,
ExpiresUtc = DateTimeOffset.UtcNow.AddHours(1),
IsPersistent = false
};
const string authenticationType = "Cookies";
var claimsIdentity = new ClaimsIdentity(claims, authenticationType);
await _httpContextAccessor.HttpContext.SignInAsync(authenticationType, new ClaimsPrincipal(claimsIdentity), authProperties);
}
公共异步任务AddPortalToCurrentUserClaimsAsync(Guid companyUid,Guid userUid)
{
var portal=await\u unitOfWork.Portals.All().FirstOrDefaultAsync(p=>p.CompanyUid==CompanyUid&&p.UserUid==UserUid)。ConfigureAwait(false);
if(portal==null)返回;
var索赔=新列表
{
新声明(ClaimTypes.NameIdentifier,userUid.ToString()),
新声明(CustomClaimTypes.CompanyGuid、companyUid.ToString()),
新声明(CustomClaimTypes.PortalId,portal.Id.ToString()),
新声明(CustomClaimTypes.PortalName、portal.Name)
};
var authProperties=新的AuthenticationProperties
{
IssuedUtc=DateTimeOffset.UtcNow,
ExpiresUtc=DateTimeOffset.UtcNow.AddHours(1),
ispersist=false
};
常量字符串authenticationType=“Cookies”;
var claimsIdentity=新的claimsIdentity(索赔,认证类型);
wait_httpContextAccessor.HttpContext.SignInAsync(authenticationType,newclaimsprincipal(claimsIdentity),authProperties);
}
登录后,我将添加更多索赔。我尝试了您的签名,但得到的错误是当principal.Identity.IsAuthenticated为false时,当AuthenticationOptions.RequireAuthenticatedSignIn为true时不允许签名同步。^^显示该错误的可能性最大,因为在登录之前调用了AddPortalToCurrentUserClaimsAsync。或者,当用户未登录时。我会根据某些条件(有时不是)在AuthenticateSarAsync之后立即运行AddPortalToCurrentUserClaimsAsync。如果要修改
AuthenticateSarAsync
方法以包含门户声明,该怎么办?正在寻找此问题的解决方案
public async Task AddPortalToCurrentUserClaimsAsync(Guid companyUid, Guid userUid)
{
var portal = await _unitOfWork.Portals.All().FirstOrDefaultAsync(p => p.CompanyUid == companyUid && p.UserUid == userUid).ConfigureAwait(false);
if (portal == null) return;
var claims = new List<Claim>
{
new Claim(CustomClaimTypes.PortalId, portal.Id.ToString()),
new Claim(CustomClaimTypes.PortalName, portal.Name)
};
var claimsIdentity = new ClaimsIdentity(claims);
_httpContextAccessor.HttpContext.User.AddIdentity(claimsIdentity);
}
var portalId = _httpContextAccessor.HttpContext.User.FindFirst(CustomClaimTypes.PortalId);
var portalName = _httpContextAccessor.HttpContext.User.FindFirst(CustomClaimTypes.PortalName);
public async Task AddPortalToCurrentUserClaimsAsync(Guid companyUid, Guid userUid)
{
var portal = await _unitOfWork.Portals.All().FirstOrDefaultAsync(p => p.CompanyUid == companyUid && p.UserUid == userUid).ConfigureAwait(false);
if (portal == null) return;
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, userUid.ToString()),
new Claim(CustomClaimTypes.CompanyGuid, companyUid.ToString()),
new Claim(CustomClaimTypes.PortalId, portal.Id.ToString()),
new Claim(CustomClaimTypes.PortalName, portal.Name)
};
var authProperties = new AuthenticationProperties
{
IssuedUtc = DateTimeOffset.UtcNow,
ExpiresUtc = DateTimeOffset.UtcNow.AddHours(1),
IsPersistent = false
};
const string authenticationType = "Cookies";
var claimsIdentity = new ClaimsIdentity(claims, authenticationType);
await _httpContextAccessor.HttpContext.SignInAsync(authenticationType, new ClaimsPrincipal(claimsIdentity), authProperties);
}