C
C#2y ago
Dreams

Help setting up ASP.NET Core Identity and SQL Server

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using SalonML_API.Data;
using SalonML_API.Data.Models;

namespace SalonML_API.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class SeedController : ControllerBase
{
private readonly ApplicationDbContext _context;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly UserManager<ApplicationUser> _userManager;
private readonly IConfiguration _configuration;

public SeedController(
ApplicationDbContext context,
RoleManager<IdentityRole> roleManager,
UserManager<ApplicationUser> userManager,
IConfiguration configuration)
{
_context = context;
_roleManager = roleManager;
_userManager = userManager;
_configuration = configuration;
}

[HttpGet]
public async Task<IActionResult> ImportLoremIpsum()
{
throw new NotImplementedException();
}

[HttpGet]
public async Task<IActionResult> CreateDefaultUsers()
{
// create the Administrator role
string role_Administrator = "Administrator";

if (await _roleManager.FindByNameAsync(role_Administrator) == null)
await _roleManager.CreateAsync(new IdentityRole(role_Administrator));
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using SalonML_API.Data;
using SalonML_API.Data.Models;

namespace SalonML_API.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class SeedController : ControllerBase
{
private readonly ApplicationDbContext _context;
private readonly RoleManager<IdentityRole> _roleManager;
private readonly UserManager<ApplicationUser> _userManager;
private readonly IConfiguration _configuration;

public SeedController(
ApplicationDbContext context,
RoleManager<IdentityRole> roleManager,
UserManager<ApplicationUser> userManager,
IConfiguration configuration)
{
_context = context;
_roleManager = roleManager;
_userManager = userManager;
_configuration = configuration;
}

[HttpGet]
public async Task<IActionResult> ImportLoremIpsum()
{
throw new NotImplementedException();
}

[HttpGet]
public async Task<IActionResult> CreateDefaultUsers()
{
// create the Administrator role
string role_Administrator = "Administrator";

if (await _roleManager.FindByNameAsync(role_Administrator) == null)
await _roleManager.CreateAsync(new IdentityRole(role_Administrator));
I get an error on the second to last line "Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid object name 'AspNetRoles'." I'm looking at the SSMS and there are no databases/tables related to users... I was expecting the databases and tables to be made automatically if it finds them missing Is there some kind of configuration I need to do?
5 Replies
Dreams
DreamsOP2y ago
this is my Program.cs:
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Add ApplicationDbContext and SQL Server support
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection"))
);

// Add ASP.NET Core Identity support
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireUppercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequiredLength = 8;
})
.AddEntityFrameworkStores<ApplicationDbContext>();

var app = builder.Build();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

// Add ApplicationDbContext and SQL Server support
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection"))
);

// Add ASP.NET Core Identity support
builder.Services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireUppercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequiredLength = 8;
})
.AddEntityFrameworkStores<ApplicationDbContext>();

var app = builder.Build();
Saber
Saber2y ago
it doesn't create the tables unless you have migrations and tell it to run them
Dreams
DreamsOP2y ago
I tried Update-Database and it told me "No migrations were applied. The database is already up to date."
Saber
Saber2y ago
did you create any migrations
Dreams
DreamsOP2y ago
@Deluxe thank you i forgot i need to create migrations before Update-Database i think it's working
Want results from more Discord servers?
Add your server