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。净的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. How do we change the database in which the UserManager stores user data?
在解决方案资源管理器> App_Start > Startup.Auth中。有以下配置ASP的代码。净单位。如何更改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
};
}
2 个解决方案
#1
7
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.
另一种方法是使用连接字符串的名称作为上下文构造函数的参数,并将其传递给基本构造函数。
#2
5
Pass your own DbContext
to the UserStore
constructor or change the Web.config connection string named DefaultConnection
. Either way the comments by @ta.speot.is are correct.
将自己的DbContext传递给UserStore构造函数或更改Web。配置连接字符串命名为DefaultConnection。不管怎样,@ta.speot的评论。是正确的。
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. In code, we configure it to store user data as type IdentityUser
in the MyDbContext
data store.
UserStore类公开一个非常基本的用户管理api。在代码中,我们将其配置为将用户数据作为类型IdentityUser存储在MyDbContext数据存储中。
The UserManager
class exposes a higher level user management api that automatically saves changes to the UserStore
. In code, we configure it to use the UserStore
that we just created.
UserManager类公开一个更高级别的用户管理api,该api自动保存对UserStore的更改。在代码中,我们将其配置为使用刚刚创建的UserStore。
The UserManagerFactory
should implement the factory pattern in order to get one instance of UserManager
per request for the application. Otherwise you will get the following exception:
UserManagerFactory应该实现工厂模式,以便为每个应用程序请求获取一个UserManager实例。否则,您将得到以下异常:
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. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.
在创建模型时不能使用上下文。如果在onmodelcreation方法中使用上下文,或者同一上下文实例被多个线程同时访问,则可能引发此异常。注意,DbContext和相关类的实例成员不能保证是线程安全的。
That's all.
这是所有。
#1
7
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.
另一种方法是使用连接字符串的名称作为上下文构造函数的参数,并将其传递给基本构造函数。
#2
5
Pass your own DbContext
to the UserStore
constructor or change the Web.config connection string named DefaultConnection
. Either way the comments by @ta.speot.is are correct.
将自己的DbContext传递给UserStore构造函数或更改Web。配置连接字符串命名为DefaultConnection。不管怎样,@ta.speot的评论。是正确的。
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. In code, we configure it to store user data as type IdentityUser
in the MyDbContext
data store.
UserStore类公开一个非常基本的用户管理api。在代码中,我们将其配置为将用户数据作为类型IdentityUser存储在MyDbContext数据存储中。
The UserManager
class exposes a higher level user management api that automatically saves changes to the UserStore
. In code, we configure it to use the UserStore
that we just created.
UserManager类公开一个更高级别的用户管理api,该api自动保存对UserStore的更改。在代码中,我们将其配置为使用刚刚创建的UserStore。
The UserManagerFactory
should implement the factory pattern in order to get one instance of UserManager
per request for the application. Otherwise you will get the following exception:
UserManagerFactory应该实现工厂模式,以便为每个应用程序请求获取一个UserManager实例。否则,您将得到以下异常:
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. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.
在创建模型时不能使用上下文。如果在onmodelcreation方法中使用上下文,或者同一上下文实例被多个线程同时访问,则可能引发此异常。注意,DbContext和相关类的实例成员不能保证是线程安全的。
That's all.
这是所有。