We have created a new ASP.NET 4.5.1 project as follows:
- Visual Studio 2013
- New Project
- Visual C#
- Web
- ASP.NET Web Application
- 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?
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
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 theUserStore
constructor or change the Web.config connection string namedDefaultConnection
. Either way the comments by @ta.speot.is are correct.Correct
Incorrect
Details
The
UserStore
class exposes a very basic user management api. In code, we configure it to store user data as typeIdentityUser
in theMyDbContext
data store.The
UserManager
class exposes a higher level user management api that automatically saves changes to theUserStore
. In code, we configure it to use theUserStore
that we just created.The
UserManagerFactory
should implement the factory pattern in order to get one instance ofUserManager
per request for the application. Otherwise you will get the following exception:That's all.