且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在用户注册期间添加声明

更新时间:2021-08-23 01:25:08

您可能已经有一个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