在 .NET 4.5.1 中设置 ASP.NET 标识连接字符串属性
本文关键字:NET 标识 连接 属性 ASP 字符串 设置 | 更新日期: 2023-09-27 18:34:46
所以基本上在最终学会了如何在.NET 4.5中将OpenAuth更改为不使用DefaultConnection之后,我转向了4.5.1,使这些学习变得毫无意义。AuthConfig.cs的职责现在驻留在Startup.Auth.cs中,OpenAuth的静态方法已被抽象化,因此我不能再直接更改OpenAuth.ConnectionString的默认值。
在 .NET 4.5.1 中更改成员资格的连接字符串/数据库的最佳做法是什么?
我遵循了您建议的方法,它对我有用。但是,很少有语法和命名问题,这些问题恰好是不同的。我认为这些差异可能是由于我们使用的不同版本的Visual Studios(而不是.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; }
}
字段 Roles
、 Users
、 UserClaims
UserLogins
、 UserRoles
是会员管理所需的建议。但是,在我的情况下,它们的类型具有不同的名称(ApplicationUser
而不是User
,IdentityUserClaim
而不是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())))
{
}
候选版本
与 Microsoft.AspNet.Identity.EntityFramework 1.0.0-rc1 配合使用
在帐户控制器的无参数构造函数中,更改行
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"))))
{
}
和你的完成。