15

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
    };
}
4
  • 2
    Doesn't UserStore have multiple constructors... Commented Jan 24, 2014 at 22:46
  • 3
    And doesn't one take a DbContext... Commented Jan 24, 2014 at 22:47
  • 1
    Yup. It does that too. Commented Jan 24, 2014 at 22:48
  • 2
    So msdn.microsoft.com/en-us/library/gg679467(v=vs.113).aspx or I believe the default constructor of UserStore will make it itself, using "DefaultConnection" for the connection string name. So you can edit that in web.config. Commented Jan 24, 2014 at 22:49

2 Answers 2

8

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

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Aha. So you make MyDbContext inherit the IdentityDbContext. What is the different between doing that and inheriting the normal DbContext?
IdentityDbContext is the context provided by ASP.NET Identity. It inherits from DbContext and adds two DbSets for the user and role management. For some more information have a look a this thread.
It doesn't work, could you please take a look at my question. thank you. stackoverflow.com/questions/21393195/…
8

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.

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.

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.

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:

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.

That's all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.