在空的ASP.NET核心Web应用程序中设置ASP.NET标识核心

时间:2016-06-03 09:08:25

标签: 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-understanding-the-basics /
  • tektutorialshub.com/asp-net-identity-tutorial-basics/%20%22ASP.Net%20Identity%20Tutoria
  • benfoster.io/blog/aspnet-identity-stripped-bare-mvc-part-1

但是,当我尝试创建数据库时,我得到了这个error

我也尝试通过在Visual Studio中模仿模板项目(ASP.NET核心Web应用程序(.Net Core))来获得相同的结果或this one

这就是我的项目的样子,它基本上是模板减去控制器,视图和模型。

Startup.cs

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?}");
        });
    }
}

ApplicationUser.cs

public class ApplicationUser : IdentityUser
{
}

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

我只想拥有一个带有asp.net标识的空项目,最好是在SQL服务器而不是在localdb中。有没有人有一个简单的指南或知道为什么它不适合我?

EDIT1

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") { }
}

EDIT2 我已经把这个项目放在github上了。

github.com/KiBlob/test

1 个答案:

答案 0 :(得分:1)

只是一个想法,你在appsettings.json文件中定义了DefaultConnection吗?

我的样子如下:

&#13;
&#13;
{
  "ConnectionStrings": {
    "DefaultConnection": "Server=[SERVER];Database=[DB];Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}
&#13;
&#13;
&#13;

尝试在那里设置连接,然后再次运行Update-Database。

注意:如果解决方案中有多个项目,请确保在运行Update-Database之前,包管理器控制台中的Default项目指向设置连接的项目。

相关问题