如何在用户注册期间添加声明 [英] How to add claims during user registration

查看:33
本文介绍了如何在用户注册期间添加声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用标识为2.1.0和VS2013 U4的ASP.NET MVC 5项目.我想在注册过程中向用户添加声明,以便将其存储在数据库中.这些声明代表用户自定义属性.
在为管理员创建用于创建/编辑/删除用户的网页时,我仍在使用AccountController中的create方法来创建用户,但是我不想登录该用户.如何向用户添加这些声明?

I'm using ASP.NET MVC 5 project with identity 2.1.0 and VS2013 U4. I want to add claims to user during registration in order to be stored in db. These claims represent user custom properties.
As I created a web page for administrator to create/edit/delete users, I'm still using create method from AccountController to create a user, but I don't want to login that user. How can I add those claims to the user ?

推荐答案

您可能已经有一个UserManager类.您可以使用该用户创建用户并添加声明.

You probably already have a UserManager class. You can use that one to create users and to add claims.

以控制器中的示例为例:

As an example in a controller:

// gather some context stuff
var context = this.Request.GetContext();

// gather the user manager
var usermanager = context.Get<ApplicationUserManager>();

// add a country claim (given you have the userId)
usermanager.AddClaim("userid", new Claim(ClaimTypes.Country, "Germany"));

为此,您需要实现自己的UserManager并将其与OWIN上下文链接(在示例中,它是ApplicationUserManager,基本上是class ApplicationUserManager : UserManager<ApplicationUser> { },只添加了少量配置).可在此处进行一些阅读: https://msdn.microsoft.com/en-us/library/dn613290%28v=vs.108%29.aspx

In order for this to work you need to implement your own UserManager and link it with the OWIN context (in the example it's ApplicationUserManager which basically is class ApplicationUserManager : UserManager<ApplicationUser> { } with only a small amount of configuration added). A bit of reading is available here: https://msdn.microsoft.com/en-us/library/dn613290%28v=vs.108%29.aspx

这篇关于如何在用户注册期间添加声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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