简体   繁体   English

更改ASP.NET Identity存储用户数据的数据库

[英]Change the database in which ASP.NET Identity stores user data

We have created a new ASP.NET 4.5.1 project as follows: 我们创建了一个新的ASP.NET 4.5.1项目,如下所示:

  • Visual Studio 2013 Visual Studio 2013
  • New Project 新项目
  • Visual C# Visual C#
  • Web 卷筒纸
  • ASP.NET Web Application ASP.NET Web应用程序
  • Web API Web API
  • Change Authentication 更改身份验证
  • Individual User Accounts 个人用户帐户
  • Okay > Okay 好的>好的

In the solution explorer > App_Start > Startup.Auth.cs file there is the following code which configures ASP.NET Indentity. 在解决方案资源管理器> App_Start> Startup.Auth.cs文件中,有以下代码用于配置ASP.NET Indentity。 How do we change the database in which the UserManager stores user data? 我们如何更改UserManager存储用户数据的数据库?

static Startup()
{
    PublicClientId = "self";

    UserManagerFactory = () => new UserManager<IdentityUser>(new UserStore<IdentityUser>());

    OAuthOptions = new OAuthAuthorizationServerOptions
    {
        TokenEndpointPath = new PathString("/Token"),
        Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
        AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
        AllowInsecureHttp = true
    };
}

Additionally to what @ta.speot.is and @Shaun mentioned: You can also pass the name of the connection string (stored in the web.config) in your context to the base constructor of the IdentityDbContext 除了@ ta.speot.is和@Shaun提到的内容之外:您还可以将上下文中的连接字符串(存储在web.config中)的名称传递给IdentityDbContext的基础构造函数

public class MyDbContext : IdentityDbContext<MyUser>
{
  public MyDbContext()
    : base("TheNameOfTheConnectionString")
  {
  }
}

This tutorial contains an extensive example. 教程包含一个广泛的示例。

Another way would be to use the name of the connection string as a parameter of your context constructor and pass it to the base constructor. 另一种方法是使用连接字符串的名称作为上下文构造函数的参数,并将其传递给基础构造函数。

Pass your own DbContext to the UserStore constructor or change the Web.config connection string named DefaultConnection . 将您自己的DbContext传递给UserStore构造函数或更改名为DefaultConnection的Web.config连接字符串。 Either way the comments by @ta.speot.is are correct. 无论哪种方式,@ ta.speot.is的评论都是正确的。

Correct 正确

// do this - it's the factory pattern
UserManagerFactory 
= () => new UserManager<IdentityUser>(new UserStore<IdentityUser>(new MyDbContext()));

Incorrect 不正确

// do NOT do this - use the preceding code. 
var userStore = new UserStore<IdentityUser>(new MyDbContext());                       
var userManager = new UserManager<IdentityUser>(userStore);
UserManagerFactory = () => userManager;

Details 细节

The UserStore class exposes a very basic user management api. UserStore类公开了一个非常基本的用户管理API。 In code, we configure it to store user data as type IdentityUser in the MyDbContext data store. 在代码中,我们将其配置为在MyDbContext数据存储MyDbContext用户数据存储为IdentityUser类型。

The UserManager class exposes a higher level user management api that automatically saves changes to the UserStore . UserManager类公开更高级别的用户管理API,自动将更改保存到UserStore In code, we configure it to use the UserStore that we just created. 在代码中,我们将其配置为使用我们刚刚创建的UserStore

The UserManagerFactory should implement the factory pattern in order to get one instance of UserManager per request for the application. UserManagerFactory应实现工厂模式,以便为应用程序的每个请求获取一个UserManager实例。 Otherwise you will get the following exception: 否则您将获得以下异常:

The context cannot be used while the model is being created. 在创建模型时不能使用上下文。 This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. 如果在OnModelCreating方法中使用上下文,或者同时由多个线程访问相同的上下文实例,则可能抛出此异常。 Note that instance members of DbContext and related classes are not guaranteed to be thread safe. 请注意,DbContext和相关类的实例成员不保证是线程安全的。

That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM