Memhave Memhave - 3 months ago 32
MySQL Question

Web API 2.0 Using MySQL Identity Provider, AuthContext?

I am attempting to setup token authentication using ASP.NET Identity in a WebAPI 2 with a MySQL Backend using this tutorial.

http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/

Unfortunately, this example uses Entity Framework and I am attempting to substitute it where ever I can (if it is even possible) with MySQL.

I have so far come to step 5, creating an authentication repository. This is the example given:

public class AuthRepository : IDisposable
{
private AuthContext _ctx;

private UserManager<IdentityUser> _userManager;

public AuthRepository()
{
_ctx = new AuthContext();
_userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(_ctx));
}

public async Task<IdentityResult> RegisterUser(UserModel userModel)
{
IdentityUser user = new IdentityUser
{
UserName = userModel.UserName
};

var result = await _userManager.CreateAsync(user, userModel.Password);

return result;
}

public async Task<IdentityUser> FindUser(string userName, string password)
{
IdentityUser user = await _userManager.FindAsync(userName, password);

return user;
}

public void Dispose()
{
_ctx.Dispose();
_userManager.Dispose();

}
}


I substituted UserManager for MySQLUserManager, but I cannot make an AuthContext.
Does anybody know what I must reference, or replace.

Maybe I do not even need to reference it, as in my IdentityConfig.cs file I only have:

//Old entity framework stuff
//var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));

//shiny new mysql stuff
var manager = new ApplicationUserManager(new MySqlUserStore<ApplicationUser>());


(based off this tutorial http://blog.developers.ba/asp-net-identity-2-1-for-mysql/)

If I need an application user, should I pass it somehow?

I re-read the tutorial and came across this snippet, it appears to be a connection string of some sort.

<connectionStrings>
<add name="AuthContext" connectionString="Data Source=.\sqlexpress;Initial Catalog=AngularJSAuth;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</connectionStrings>


What could or should I replace it with?

Answer

If you want to use the Web API 2.x using a MySQL backend (NOT Entity Framework).

In the class AuthRepository, there is no need for an AuthContext, simply declare the user manager as: _userManager = new UserManager(new UserStore());

Similar to the method Create within the ApplicationUserManager, when you create a user store you reference the DbContext (in the question, this was called the AuthContext).

var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));

This was the included in the default implementation. Based off of this tutorial: http://blog.developers.ba/asp-net-identity-2-1-for-mysql/

You do not need to pass a DbContext.

(There is also a mistake in the tutorial, within the Startup.cs class this must be first, otherwise you cannot authenticate:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        HttpConfiguration config = new HttpConfiguration();
        WebApiConfig.Register(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);

    }
}

Edit: it appears there was some confusion. I changed the connection string to a MySQL Connection string, as per the Identity with MySQL tutorial linked, so it should look like this:

<add name="DefaultConnection" connectionString="Server=localhost;
Database=aspnetidentity;Uid=radenko;Pwd=somepass;"   providerName="MySql.Data.MySqlClient" />