c# - 使用MySQL身份提供程序的Web API 2.0,AuthContext?

标签 c# mysql asp.net-mvc asp.net-web-api asp.net-identity

我正在尝试使用本教程在带有MySQL后端的WebAPI 2中使用ASP.NET标识设置令牌身份验证。
http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/
不幸的是,这个例子使用了实体框架,我试图用MySQL替换它(如果可能的话)。
到目前为止,我已经进入第5步,创建一个身份验证存储库。以下是给出的示例:

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();

    }
}

我用UserManager代替MySQLUserManager,但是我不能创建AuthContext。
有人知道我必须引用或替换什么吗。
也许我甚至不需要引用它,因为在IdentityConfig.cs文件中,我只有:
        //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>());

(基于本教程)
如果我需要一个应用程序用户,我应该以某种方式传递它吗?
我重新阅读了教程,发现了这个片段,它似乎是某种类型的连接字符串。
<connectionStrings>
<add name="AuthContext" connectionString="Data Source=.\sqlexpress;Initial Catalog=AngularJSAuth;Integrated Security=SSPI;"   providerName="System.Data.SqlClient" />
</connectionStrings>

我可以或者应该用什么来代替它?

最佳答案

如果您想使用WebAPI2.x使用MySQL后端(不是实体框架)。
在类AuthRepository中,不需要AuthContext,只需将用户管理器声明为:
_userManager=新用户管理器(new UserStore());
与ApplicationUserManager中的Create方法类似,在创建用户存储时,会引用DbContext(在本问题中,这称为AuthContext)。

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

这是默认实现中包含的。基于本教程:
http://blog.developers.ba/asp-net-identity-2-1-for-mysql/
不需要传递DbContext。
(教程中还有一个错误,在Startup.cs类中,这必须是第一个,否则您无法验证:
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);

    }
}

编辑:似乎有些混乱。我把连接字符串改成了MySQL连接字符串,就像链接MySQL教程的Identity一样,应该是这样的:
<add name="DefaultConnection" connectionString="Server=localhost;
Database=aspnetidentity;Uid=radenko;Pwd=somepass;"   providerName="MySql.Data.MySqlClient" />

关于c# - 使用MySQL身份提供程序的Web API 2.0,AuthContext?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38855252/

相关文章:

c# - 正则表达式上的多行

c# - ThreadPool 设置是全局设置还是仅针对工作项?

C# 分隔参数并将其传递给 Process.Start() 方法

来自审计跟踪数据库的 Mysql 用户事件报告

java - 安卓注册登录app报错

c# - 如何使用 Autofac 根据用户上下文解析组件

c# - "Static"C#应用配置文件加密

php - 我在表中有两列,如品牌和产品,那么如何防止相同品牌插入相同产品?

c# - Action 参数命名

asp.net-mvc - MVCContrib - 测试 XMLResult 的最佳方法是什么?