Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net HttpContext.Current在重新生成标识期间为null_Asp.net_Asp.net Identity_Identity_Claims Based Identity_Asp.net Identity 2 - Fatal编程技术网

Asp.net HttpContext.Current在重新生成标识期间为null

Asp.net HttpContext.Current在重新生成标识期间为null,asp.net,asp.net-identity,identity,claims-based-identity,asp.net-identity-2,Asp.net,Asp.net Identity,Identity,Claims Based Identity,Asp.net Identity 2,简介: 我正在构建Identity 2.0的自定义实现。默认情况下,框架每30分钟刷新一次用户身份,创建一个新的ClaimsIdentity。因为我有一些自定义声明(我在登录方法中设置),我想在刷新时转移到新的ClaimsIdentity,所以我想出了一种方法,从HttpContext读取当前的ClaimsIdentity,并将其作为“新的”ClaimsIdentity返回。问题在于刷新标识时,HttpContext.Current为空,因此我无法将旧声明复制到新标识 代码: 在我的Startu

简介:

我正在构建Identity 2.0的自定义实现。默认情况下,框架每30分钟刷新一次用户身份,创建一个新的
ClaimsIdentity
。因为我有一些自定义声明(我在登录方法中设置),我想在刷新时转移到新的
ClaimsIdentity
,所以我想出了一种方法,从
HttpContext
读取当前的
ClaimsIdentity
,并将其作为“新的”
ClaimsIdentity
返回。问题在于刷新标识时,
HttpContext.Current
为空,因此我无法将旧声明复制到新标识

代码:

在我的
Startup.cs
文件中,我有一个每隔x分钟刷新用户身份的代码:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    Provider = new CookieAuthenticationProvider
    {
        OnValidateIdentity = SecurityStampValidatorExtensions.OnValidateIdentity(
            validateInterval: TimeSpan.FromMinutes(0.5),
            regenerateIdentity: (manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie))
    }
});
RegenerateIdentity
func在my
UserManager
中调用此方法:

public async override Task<ClaimsIdentity> CreateIdentityAsync(ApplicationUser user, string authenticationType)
{
    ClaimsIdentity claimsIdentity;
    if (HttpContext.Current != null && 
        HttpContext.Current.User != null && 
        HttpContext.Current.User.Identity != null && 
        HttpContext.Current.User.Identity.IsAuthenticated)
    {
        // Just return the existing ClaimsIdentity so we don't lose our custom claims
        claimsIdentity = (ClaimsIdentity)HttpContext.Current.User.Identity;

        // TODO refresh some claims from the database

        return claimsIdentity;
    }

    // Create a new ClaimsIdentity if none exists
    claimsIdentity = await ClaimsIdentityFactory.CreateAsync(this, user, authenticationType);
    claimsIdentity.AddClaim(new Claim(Constants.DefaultSecurityStampClaimType, await GetSecurityStampAsync(user.Id)));

    return claimsIdentity;
}
公共异步覆盖任务CreateIdentityAsync(ApplicationUser用户,字符串身份验证类型)
{
请求权请求权;
if(HttpContext.Current!=null&&
HttpContext.Current.User!=null&&
HttpContext.Current.User.Identity!=null&&
HttpContext.Current.User.Identity.IsAuthenticated)
{
//只需返回现有索赔实体,这样我们就不会丢失自定义索赔
claimsIdentity=(claimsIdentity)HttpContext.Current.User.Identity;
//TODO刷新数据库中的某些声明
返还请求权;
}
//如果不存在索赔实体,则创建新的索赔实体
claimsIdentity=await ClaimsIdentityFactory.CreateAsync(this,user,authenticationType);
AddClaim(新的Claim(Constants.DefaultSecurityStampClaimType,wait GetSecurityStampAsync(user.Id));
返还请求权;
}
问题:

第一次调用
CreateIdentityAsync
时(当我登录时),
HttpContext.Current
有一个值,标识被创建,一切正常。问题是:当再次调用
CreateIdentityAsync
时(因为正在刷新标识),
HttpContext.Current
null
。我不明白这是为什么,我还没能解决它

理论:

我关于为什么
HttpContext.Current
null
的一些理论:

  • 与异步和未传输到新线程的
    HttpContext
    有关吗?我尝试构建自己的等待器,以便在
    HttpContext.Current
    上进行复制,但没有成功
  • HttpContext
    正在被框架主动破坏,以确保它摆脱一切?我还没有找到任何这样做的代码,所以看起来不太可能

  • 有人对如何实现这一点有什么建议吗?

    cookie中间件在IIS管道中的身份验证阶段运行,该阶段在HttpContext或会话状态可用之前。所以你需要在没有它的情况下工作