【问题标题】:Set ASP.NET Identity ConnectionString property in .NET 4.5.1在 .NET 4.5.1 中设置 ASP.NET Identity ConnectionString 属性
【发布时间】:2013-10-18 23:48:19
【问题描述】:

所以基本上在最终学习how to change OpenAuth to not use DefaultConnection in .NET 4.5 之后,我已经转向 4.5.1,使这些学习变得毫无意义。 AuthConfig.cs 的职责现在驻留在 Startup.Auth.cs 中,OpenAuth 的静态方法已经被抽象掉,因此我不能再直接更改 OpenAuth.ConnectionString 的默认值。

在 .NET 4.5.1 中更改 Membership 的连接字符串/数据库的最佳做法是什么?

【问题讨论】:

    标签: c# asp.net visual-studio-2013


    【解决方案1】:

    我遵循了您建议的方法,它对我有用。然而,几乎没有什么东西,主要是语法和命名问题,碰巧是不同的。我认为这些差异可能是由于我们使用的 Visual Studio 版本不同(而不是 .NET - 我的版本是 .NET 4.5.1 的发行版)。我继续描述我的具体解决方案。

    我的目标是拥有一个数据库上下文,通过它我可以访问用户或身份相关数据以及我的自定义应用程序数据。为了实现这一点,我完全删除了 ApplicationDbContext 类,它会在您创建新项目时自动为您创建。

    然后,我创建了一个新类MyDbContext

    public class MyDbContext: DbContext
    {
        public MyDbContext() : base("name=DefaultConnection")
        {
    
        }
    
        //
        // These are required for the integrated user membership.
        //
        public virtual DbSet<IdentityRole> Roles { get; set; }
        public virtual DbSet<ApplicationUser> Users { get; set; }
        public virtual DbSet<IdentityUserClaim> UserClaims { get; set; }
        public virtual DbSet<IdentityUserLogin> UserLogins { get; set; }
        public virtual DbSet<IdentityUserRole> UserRoles { get; set; }
    
        public DbSet<Movie> Movies { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<Purchase> Purchases { get; set; }
    }
    

    RolesUsersUserClaimsUserLoginsUserRoles 字段是会员管理所需的建议字段。但是,在我的例子中,它们的类型有不同的名称(ApplicationUser 而不是 UserIdentityUserClaim 而不是 UserClaim 等等)。我想这就是Antevirus出现“找不到用户”问题的原因。

    另外,正如我们在我的例子中看到的那样,有 5 个这样的字段,而不是 8 个。这可能是由于 Visual Studio 的不同版本。

    我所做的最后一个更改是在AccountController 类中,它反映了新上下文MyDbContext 的使用。这里我传递了MyDbContext 的实例而不是ApplicationDbContext

    之前

    public AccountController()
        : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
    {
    }
    

    之后

    public AccountController()
        : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new MyDbContext())))
    {
    }
    

    【讨论】:

    • 看看我编辑的答案。我相信它更容易使用,也更安全。我之前的回答确实将两个 DbContext 合二为一,但它从 IdentityDbContext 中遗漏了一些可能不应该删除的代码。
    【解决方案2】:

    发布候选人

    适用于 Microsoft.AspNet.Identity.EntityFramework 1.0.0-rc1

    在 AccountController 的无参数构造函数中,更改行

    IdentityManager = new AuthenticationIdentityManager(new IdentityStore());
    

    IdentityManager = new AuthenticationIdentityManager(new IdentityStore(new DefaultIdentityDbContext("YourNameOrConnectionString")));
    

    你可以走了。

    发布

    适用于 Microsoft.AspNet.Identity.EntityFramework 1.0.0

    与我们为候选发布版所做的类似,但我们在不同的地方执行此操作。打开作为 VS 模板的一部分创建的 IdentityModels.cs,并将以下构造函数添加到 ApplicationDbContext 类:

    public ApplicationDbContext(string nameOrConnectionString)
        : base(nameOrConnectionString)
    {
    }
    

    您现在可以将AccountController 中的无参数构造函数从

    public AccountController()
        : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
    {
    }
    

    public AccountController()
        : this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext("YourNameOrConnectionString"))))
    {
    }
    

    你完成了。

    【讨论】:

    • 你好。我将这些添加到我自己的上下文中,但我得到“找不到类型或命名空间名称'用户'(您是否缺少 using 指令或程序集引用?)”。所有添加的 DbSet 也是如此。 Microsoft.AspNet.Identity.EntityFramework 作为参考和使用指令添加。有什么想法吗?
    • @Antevirus 看看我提供的新代码。您正在使用 Microsoft.AspNet.Identity.EntityFramework 的发布版本,这就是您收到错误的原因;在库的发布版本中,用户被重命名为 IdentityUser。从 1.0.0-rc1 到 1.0.0,AccountController 及其所依赖的类发生了重大变化。我提供的新代码确实为您的帐户模型和域模型留下了单独的 dbcontexts(有时很难避免它们重叠),但它更简单、更安全。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2013-11-17
    • 2016-07-23
    • 1970-01-01
    • 2014-06-25
    相关资源
    最近更新 更多