asp.net - 添加并保留 "temporary"声明

标签 asp.net asp.net-mvc asp.net-identity-2

在 MVC5 Web 应用程序上,我使用 Asp.net Identity。当用户注册时,我添加一些声明,它们保存在数据库中并在用户登录时恢复。这非常有效。现在,基于参数(登录页面上的复选框),我想在用户登录时向用户添加特定的声明。但有一个问题:此声明仅存在于该用户特定的 session 中(如果同一用户登录另一个浏览器实例或设备并且不选中该复选框,他将不会拥有该声明)。我不使用也不希望依赖 asp.net Session。

我很容易实现这一点,只需在调用AuthenticationManager.SignIn时添加声明即可:

private async Task SignInAsync(CustomUser user, bool isPersistent, bool myCustomTemporaryClaim)
{
    var identities = await user.GenerateUserIdentityAsync(UserManager);

    if (myCustomTemporaryClaim)
    {
        identities.AddClaim(new Claim(CustomClaimTypes.MyCustomTemporaryClaim, "true"));
    }

    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identities);
}

这很好用。但 VS 模板上的默认 asp.net 身份实现被配置为每 30 分钟“刷新”一次身份。发生这种情况时,我将失去我的海关 claim 。那么,我想知道的是,在 ASP.NET 身份重新生成 Cookie 之前,是否可以“拦截”并获取我的自定义声明值?

我可以删除regenerateIdentityCallback,但我不知道这样做的结果可能是什么。

最佳答案

我确信您现在已经知道要做什么,但为了以防万一其他人偶然发现此线程,我认为您只需将 AddClaim 移动到GenerateUserIdentityAsync 方法中即可。然后,当刷新发生并调用 regenerateIdentityCallback 时,您的声明将被添加回来。

对于您的条件 myCustomTemporaryClaim,您可以在GenerateUserIdentityAsync 中包含一个参数。有关如何执行此操作以及如何更改回调的更多详细信息,请参阅这篇文章: ExpireTimeSpan ignored after regenerateIdentity / validateInterval duration in MVC Identity (2.0.1)

即 (请注意,我使用 int 作为我的 UserId)

private async Task SignInAsync(ApplicationUser user, bool isPersistent, bool myCustomTemporaryClaim)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await user.GenerateUserIdentityAsync(UserManager, myCustomTemporaryClaim);
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>, IUser<int>
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, int> manager, bool myCustomTemporaryClaim)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        if (myCustomTemporaryClaim)
        {
            userIdentity.AddClaim(new Claim(CustomClaimTypes.MyCustomTemporaryClaim, Convert.ToString(true)));
        }

        return userIdentity;
    }
}

顺便说一句,我最终阅读您的文章的原因是因为我在调用 SignInAsync 时不断丢失自定义声明,所以我只是替换了这个

var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

有了这个

var identity = await user.GenerateUserIdentityAsync(UserManager);

关于asp.net - 添加并保留 "temporary"声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23832423/

相关文章:

c# - 具有初始金额的定期 paypal 付款的空引用

asp.net - 同时从2个表中删除?

c# - 如何将 cs 文件添加并绑定(bind)到没有 ASCX/ASPX 页面的页面?

悬停按钮 ASP.net 上的 JQuery 更改类

javascript - 为什么当我尝试在 JavaScript 中解析 JSON 时出现错误

c# - 为什么 Await 不会出现阻止对 EF 上下文的第二次操作

javascript - MVC 3 中带有 jQ​​uery 的低调 JavaScript

asp.net - 将 Controller Action 权限动态分配给 asp.net MVC 中的角色

asp.net-mvc - 我有角色种子 - 但 Roles.GetAllRoles 不起作用?

c# - Identity 2.0.0 中的 ApplicationUser 和 ApplicationRole 导航属性