Asp.net core 在空的ASP.NET Core Web应用程序中设置ASP.NET Identity Core
我正在尝试启动一个新的web应用程序项目,我想使用asp.net标识数据库(包含所有AspNet表(AspNetUsers、AspNetRoles等)的数据库) 我曾尝试遵循许多指南,其中包括:Asp.net core 在空的ASP.NET Core Web应用程序中设置ASP.NET Identity Core,asp.net-core,asp.net-identity,asp.net-core-mvc,Asp.net Core,Asp.net Identity,Asp.net Core Mvc,我正在尝试启动一个新的web应用程序项目,我想使用asp.net标识数据库(包含所有AspNet表(AspNetUsers、AspNetRoles等)的数据库) 我曾尝试遵循许多指南,其中包括: bitoftech.net/2015/01/21/asp-net-identity-2-with-asp-net-web-api-2-accounts-management/ johnatten.com/2014/04/20/asp-net-mvc-and-identity-2-0-CONTRUMEN
- bitoftech.net/2015/01/21/asp-net-identity-2-with-asp-net-web-api-2-accounts-management/
- johnatten.com/2014/04/20/asp-net-mvc-and-identity-2-0-CONTRUMENT-the-basics/
- tektutorialshub.com/asp网络身份教程基础知识/%20%22ASP.net%20Identity%20Tutoria
- benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
//var connectionString = @"Data Source=(localdb)\mssqllocaldb;Initial Catalog=Northwind;Integrated Security=True;Pooling=False";
//services.AddEntityFramework()
// .AddSqlServer()
// .AddDbContext<NorthwindContext>(o =>
// o.UseSqlServer(connString));
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
app.UseDeveloperExceptionPage();
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
ApplicationDbContext.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
public类ApplicationDbContext:IdentityDbContext
{
公共应用程序DBContext(DbContextOptions选项)
:基本(选项)
{
}
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
//自定义ASP.NET标识模型,并根据需要覆盖默认值。
//例如,可以重命名ASP.NET标识表名称等。
//在调用base.OnModelCreating(builder)后添加自定义项;
}
}
我只想有一个asp.net标识的空项目,最好是在SQL server中,而不是在localdb中。有没有人有一个简单的指南,或者知道为什么它不适合我
编辑1
public类ApplicationDbContext:IdentityDbContext
{
public ApplicationDbContext():此(“数据源=ACLAP;初始目录=tmpCore;集成安全性=True;连接超时=15;加密=False;信任服务器证书=True;applicationcontent=读写;多子网故障转移=False”){}
}
编辑2
我已经在github上完成了这个项目
github.com/KiBlob/test只是一个想法,您是否在appsettings.json文件中定义了DefaultConnection 我的看起来像这样:
{
“连接字符串”:{
“DefaultConnection”:“服务器=[Server];数据库=[DB];受信任的连接=True;MultipleActiveResultSets=True”
},
“日志记录”:{
“包含范围”:错误,
“日志级别”:{
“默认值”:“调试”,
“系统”:“信息”,
“Microsoft”:“信息”
}
}
}
我认为您的代码没有任何问题。您的VisualStudio似乎有些奇怪。这是将迁移更新推送到数据库的命令。当您在PackageManager控制台中键入时,应该会看到一个选项下拉列表。另外,可能值得注意的是,它是“有点”capsensive,因此当我执行“添加迁移初始创建”或“更新数据库”时,我会尝试更新数据库。我收到以下错误消息“System.ArgumentNullException:值不能为null。参数名称:connectionString”但我基本上是从VS模板复制了它抱怨的部分。您的Web配置中有连接字符串吗?@JasonH没有,但我复制的VS模板也没有。即使使用web配置中的连接字符串,我也会遇到相同的错误。请尝试将DBContext构造函数更改为:public ApplicationDbContext():this(“myConnectionString”){}我到处都可以看到结构数据->DefaultConnection->ConnectionString。这个答案显示了appsettings.json格式,它实际上允许我从DNX 1.0中的Configuration.GetConnectionString(“DefaultConnection”)中提取连接字符串。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : this("Data Source=ACLAP;Initial Catalog=tmpCore;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False") { }
}