Reputation: 385
I'm stuck in authentication in my project. I created asp.net mvc project without authentication. I have existing database. I added db by executing: Scaffold-Dbcontext
and use this in Startup.cs(ConfigureServices method):
services.AddEntityFrameworkSqlServer().AddDbContext<AppContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("SqlDb")));
For Identity i added this in ConfigureServices:
services.AddIdentity<IdentityUser, IdentityRole>()
.AddEntityFrameworkStores<AppContext>()
.AddDefaultTokenProviders();
and this in Configure:
app.UseAuthentication();
I have next controller:
private readonly UserManager<IdentityUser> _userManager;
private readonly SignInManager<IdentityUser> _signInManager;
private readonly AppContext _context;
public HomeController(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager, AppContext context)
{
this._userManager = userManager;
this._signInManager = signInManager;
_context = context;
}
And when i try to find user by id:
var usr = _userManager.FindByIdAsync(user.Id);
App throw this exception :System.InvalidOperationException: 'Cannot create a DbSet for 'IdentityUser' because this type is not included in the model for the context.'
Change inherit in context to IdentityDbContext throw 3 new exceptions, one of them is :
'App.Chat.Identity.ApplicationUser' cannot be used as type parameter 'TUser' in the generic type or method 'IdentityDbContext<TUser>'. There is no implicit reference conversion from 'App.Chat.Identity.ApplicationUser' to 'Microsoft.AspNet.Identity.EntityFramework.IdentityUser'.
Can someone explain how to use authentication in empty project? Or i need to recreate project with individual auth? (Second varian will hurt me).
Edit #1 Here is additional info:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
namespace App.Chat.Identity
{
public class ApplicationUser : IdentityUser
{
}
}
And startup info:
services.AddEntityFrameworkSqlServer().AddDbContext<AppContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("SqlDb")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppContext>()
.AddDefaultTokenProviders();
Edit #2 My AppContext:
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using App.Chat.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
namespace App.Chat.TModels
{
public partial class AppContext : IdentityDbContext<ApplicationUser>
{
public AppContext(DbContextOptions<AppContext> options)
: base(options)
{ }
}
Upvotes: 2
Views: 900
Reputation: 308
Like Kahbazi suggested change name. And check these 2 classes.
//change in startup.
//services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationDbContext>(options =>
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 ApplicationUser : IdentityUser
{
}
public class HomeController
{
private readonly UserManager<IdentityUser> _userManager;
private readonly SignInManager<IdentityUser> _signInManager;
private readonly ApplicationDbContext _context;
public HomeController(UserManager<IdentityUser> userManager, SignInManager<IdentityUser> signInManager, ApplicationDbContext context)
{
this._userManager = userManager;
this._signInManager = signInManager;
_context = context;
}
}
Upvotes: 1
Reputation: 15015
Your error says Microsoft.AspNet.Identity.EntityFramework.IdentityUser
which is not a ASP.NET Core package. It's for the old framework.
Check your nuget packages and uninstall Microsoft.AspNet.Identity.EntityFramework
and Microsoft.AspNet.Identity.Core
Upvotes: 1