【问题标题】:Reuse Claim in regenerateIdentityCallback in Owin Identity in MVC5在 MVC5 中的 Owin Identity 中重用声明 regenerateIdentityCallback
【发布时间】:2014-07-30 17:06:27
【问题描述】:

我正在使用带有 Owin 身份的 MVC5。

我正在努力在 regenerateIdentityCallback 中重用任何自定义声明。

我在启动时有这个配置(在新 MVC 项目的标准模板中提供)

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, Guid>(
                    validateInterval: TimeSpan.FromSeconds(10),
                    regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
                    getUserIdCallback: (user) => Guid.Parse(user.GetUserId()))
            }
        });

GenerateUserIdentityAsync 看起来像这样:(以及模板中的标准)

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, Guid> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);

        // I want here instead of generating new one, just reuse the previous one
        userIdentity.AddClaim(new Claim(ClaimTypes.Sid, Guid.NewGuid().ToString()));

        return userIdentity;
    }

问题是,我无法重用声明,我总是必须为它获得新的价值。在调查了我看到的 Identity dll 之后,用户的 this 实例没有声明,因为它是来自数据库的新用户,而 userIdentity 只有标准声明作为 Id 和 UserName,它们是由 CreateIdentityAsync 方法创建的。从 HttpContext.Current 获取用户是不可能的,在这个地方它是空的。

重用声明以保留 Cookie 中的某些值的最佳方法是什么?我可能误解了索赔的目的。提前谢谢您的帮助

【问题讨论】:

    标签: asp.net-mvc asp.net-identity owin claims


    【解决方案1】:

    这样可以得到同样的结果(context.Identity是之前的身份):

    OnValidateIdentity = context => SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, DtbsUser, Guid>(
                                validateInterval: TimeSpan.FromSeconds(30),
                                regenerateIdentityCallback:
                                    (manager, user) =>
                                        user.GenerateUserIdentityAsync(manager, context.Identity),
                                getUserIdCallback: (ci) => Guid.Parse(ci.GetUserId())).Invoke(context)
    

    【讨论】:

    • 是的,谢谢!没有看到这个选项我很盲目:)我已经通过更多功能扩展了我的自定义验证器,所以我不会再替换它,但这绝对是我问题的正确答案。
    【解决方案2】:

    我放弃并创建了自己的SecurityStampValidator,它与原来的功能完全相同,但将当前的claimIdentity 作为参数传递给regenerateIdentityCallback。对此解决方案一点也不满意,但它确实有效。

           OnValidateIdentity = MySecurityStampValidator.OnValidateIdentity<ApplicationUserManager, DtbsUser, Guid>(
                        validateInterval: TimeSpan.FromSeconds(10),
                        regenerateIdentityCallback: (manager, user, previousIdentity) => user.GenerateUserIdentityAsync(manager, previousIdentity),  
                        getUserIdCallback: user => Guid.Parse(user.GetUserId()))
    

    【讨论】:

    • 能否提供“MySecurityStampValidator”的代码?谢谢
    • 不用担心,我只是复制了SecurityStampValidator的整个源代码。顺便说一句,我在 codeplex 上发布了这个问题:aspnetidentity.codeplex.com/workitem/2537
    • 看看接受的答案,实际上有很简单的方法,如何获得以前的身份:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 2016-02-28
    • 2017-02-08
    • 1970-01-01
    相关资源
    最近更新 更多