MVC5中Owin Identity中regenerateIdentityCallback中的重用声明 [英] Reuse Claim in regenerateIdentityCallback in Owin Identity in MVC5

查看:122
本文介绍了MVC5中Owin Identity中regenerateIdentityCallback中的重用声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有Owin身份的MVC5.

I am using MVC5 with Owin identity.

我正在努力在regenerateIdentityCallback中重用所有自定义声明.

I am struggling to reuse any custom Claims in regenerateIdentityCallback.

我在启动"中具有此配置(如新MVC项目的标准模板中提供的那样)

I have in Startup this configuration (as provided in the standard Template for new MVC project)

        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看起来像这样:(以及模板中的几乎所有标准)

the GenerateUserIdentityAsync looks like this: (as well pretty much standard from the Template)

    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;
    }

问题是,我无法重用Claim,而且我始终必须为其获取新的价值.在研究了Identity dll之后,我看到该用户的this实例没有声明,因为它是数据库中的新用户,并且userIdentity仅具有由CreateIdentityAsync方法创建的标准声明为ID和UserName.无法从HttpContext.Current获取用户,在此位置为null.

The problem is, that I am not able to reuse the Claim and I allways have to get new value for it. After investigation of the Identity dll I see, that this instance of the user has no Claims as it is fresh user from Database and userIdentity has only the standard Claims as Id and UserName, which are created by CreateIdentityAsync method. Getting the user from HttpContext.Current is not possible, it is null on this place.

重用Claim来从Cookie中保留某些值的最佳方法是什么?我可能误解了索赔的目的.事先感谢您的帮助

What is the best way to reuse a Claim to keep some values from the Cookie? I probably misunderstood the purpose of Claims. Thx in advance for your help

推荐答案

您可以通过执行以下操作获得相同的结果(context.Identity是先前的身份):

You can obtain the same result by doing this (context.Identity is the previous 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)

这篇关于MVC5中Owin Identity中regenerateIdentityCallback中的重用声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆