ASP.NET Unable to resolve service for type, while attempting to activate controller - asp.net

While I understand there were other questions on this very topic I was having a difficult time understanding the answers and was hoping someone could walk me through how DbContext's work as I feel I might have created a second context when I shouldn't have.
So, as I'm teaching myself more about .NET Core I'm working on turning an old school project into a .NET project which is a simple Dentist office web app where users can sign up for appointments, view their appointments, etc. I was following along with this tutorial to add additional user attributes instead of just username & e-mail as I was trying to grab the current user when creating an appointment.
Before I added this custom attributes using the default IdentityUI I had my project working where a user could register and login, create a basic appointment with their 'username' pick a date and time and once created would display their appointments in a basic table format. My next step was to add the custom user attributes so it would display based on their real-name and not their username which is defaulted to their email.
Following the tutorial I'm not sure if I misunderstood but I created a new Context and IdentityUser which all worked but it has broken my 'Appointments' page giving me the:
InvalidOperationException: Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager`1[Microsoft.AspNetCore.Identity.IdentityUser]' while attempting to activate 'WelchDentistry.Controllers.AppointmentsController'.** error.
Here is my ConfigureServices method as I believe it's an issue in regards to registering the 2 different Contexts.
public void ConfigureServices(IServiceCollection services)
{
/*
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
*/
/*
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
*/
services.AddControllersWithViews();
services.AddRazorPages();
services.AddMvc();
}
Here is the original context
namespace WelchDentistry.Data
{
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<WelchDentistry.Models.Appointment> Appointment { get; set; }
}
}
Here is my controller for my appointments
namespace WelchDentistry.Controllers
{
public class AppointmentsController : Controller
{
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityUser> _userManager;
public AppointmentsController(ApplicationDbContext context, UserManager<IdentityUser> userManager)
{
_context = context;
_userManager = userManager;
}
// GET: Appointments
public async Task<IActionResult> Index()
{
var user = await _userManager.GetUserAsync(HttpContext.User);
return View(await _context.Appointment.ToListAsync());
}
// GET: Appointments/Details/5
public async Task<IActionResult> Details(int? id)
{
if (id == null)
{
return NotFound();
}
var appointment = await _context.Appointment
.FirstOrDefaultAsync(m => m.ID == id);
if (appointment == null)
{
return NotFound();
}
return View(appointment);
}
// GET: Appointments/Create
public IActionResult Create()
{
return View();
}
// POST: Appointments/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ID, CustomerName, AppointmentTime,CustomerDoctor")] Appointment appointment)
{
if (ModelState.IsValid)
{
_context.Add(appointment);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(appointment);
}
// GET: Appointments/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if (id == null)
{
return NotFound();
}
var appointment = await _context.Appointment.FindAsync(id);
if (appointment == null)
{
return NotFound();
}
return View(appointment);
}
// POST: Appointments/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("ID,CustomerName,AppointmentTime,CustomerDoctor")] Appointment appointment)
{
if (id != appointment.ID)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(appointment);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!AppointmentExists(appointment.ID))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(appointment);
}
// GET: Appointments/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if (id == null)
{
return NotFound();
}
var appointment = await _context.Appointment
.FirstOrDefaultAsync(m => m.ID == id);
if (appointment == null)
{
return NotFound();
}
return View(appointment);
}
// POST: Appointments/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var appointment = await _context.Appointment.FindAsync(id);
_context.Appointment.Remove(appointment);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
private bool AppointmentExists(int id)
{
return _context.Appointment.Any(e => e.ID == id);
}
}
}
If more code is needed please ask or you can view on my Github
I appreciate all the help and bare with my as I'm still lost on most of this but slowly learning.

First of all remove your IdentityHostingStartup file in your Identity Area.
Then change your databasecontext to this ( You should introduce your User class ) :
public class ApplicationDbContext : IdentityDbContext<WelchDentistryUser, IdentityRole, string>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Models.Appointment> Appointment { get; set; }
}
And add this codes in your startup file .
services.AddIdentity<WelchDentistryUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
And finally you should use your custom User class in controller.
public class AppointmentsController : Controller
{
private readonly ApplicationDbContext _context;
private readonly UserManager<WelchDentistryUser> _userManager;
public AppointmentsController(ApplicationDbContext context, UserManager<WelchDentistryUser> userManager)
{
_context = context;
_userManager = userManager;
}
}

Related

Edit action is not doing anything on automapper

I´m using Visual Studio 2019, .net core 3.1 and automapper. My Edit action dont edit the record. I´ve seen tutorials but all are just of one action and I need to do a crud. Taking as example a usual edit action I´ve made this:
public class CustomerCountriesController : Controller
{
private readonly ApplicationDbContext _context;
private readonly IMapper _mapper;
public CustomerCountriesController(ApplicationDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
// GET: CustomerCountries
public async Task<IActionResult> Index()
{
//CustomerCountries customerCountry = new CustomerCountries();
var customerCountry = await _context.CustomerCountries.ToListAsync();
List<CustomerCountriesDto> countries = _mapper.Map<List<CustomerCountries>,
List<CustomerCountriesDto>>(await _context.CustomerCountries.ToListAsync());
return View(countries);
}
public async Task<IActionResult> Edit(string id)
{
if (id == null)
{
return NotFound();
}
var customerCountries = await _context.CustomerCountries.FindAsync(id);
var model = _mapper.Map<CustomerCountries, CustomerCountriesDto>(customerCountries);
if (customerCountries == null)
{
return NotFound();
}
return View(model);
//return View(customerCountries);
}
// POST: CustomerCountries/Edit/5
// To protect from overposting attacks, enable the specific properties you want to bind to, for
// more details, see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
//public async Task<IActionResult> Edit(string id, [Bind("CustomerCountry")] CustomerCountries customerCountries)
public async Task<IActionResult> Edit(string customerCountry, CustomerCountriesDto customerCountriesDto)
{
if (customerCountry != customerCountriesDto.CustomerCountry)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
var CustomerCountries = _mapper.Map<CustomerCountriesDto, CustomerCountries>(customerCountriesDto);
_context.Update(CustomerCountries);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!CustomerCountriesExists(customerCountriesDto.CustomerCountry))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
return View(customerCountriesDto);
}
public class AutoMapping : Profile
{
public AutoMapping()
{
CreateMap<CustomerCountries, CustomerCountriesDto>();
CreateMap<CustomerCountriesDto, CustomerCountries>();
}
}
public class CustomerCountries
{
[StringLength(50, ErrorMessage = "Longitud máxima para el país: 50")]
public string CustomerCountry { get; set; }
public ICollection<CustomerRegions> CustomerRegions { get; set; }
}
public class CustomerCountriesDto
{
public string CustomerCountry { get; set; }
}
On startup
services.AddAutoMapper(typeof(Startup));
The id of the table is CustomerCounty
Can you tell me the correct way?
I´ve found the solution thanks to an experienced developer that lead me through the issue and corrected my code from time to time (That is acctually really helping guys). Turns out that I was using a field as a PK: CustomerCountry... I wasn´t using and Id, when I changed the model, the update happened
var CustomerCountries = _mapper.Map<CustomerCountriesDto, CustomerCountries>(customerCountriesDto);
var country = _context.CustomerCountries.FirstOrDefault(c => c.Id == CustomerCountries.Id);
country.CustomerCountry = customerCountriesDto.CustomerCountry;
_context.Update(country);
await _context.SaveChangesAsync();

Repository signature for asp.net core 2.1 controller action return types

I'm trying to modify my Asp.net Core 2.1 project to use the new controller action return types (https://learn.microsoft.com/en-us/aspnet/core/web-api/action-return-types?view=aspnetcore-2.1#actionresultt-type)
The controller example they give is:
[HttpGet("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public ActionResult<Product> GetById(int id)
{
if (!_repository.TryGetProduct(id, out var product))
{
return NotFound();
}
return product;
}
However what does the repository signature for this method look like?
if I try:
public async Task<Product> TryGetProduct(int id)
then I get an error that there is no repository method that takes 2 arguments.
But if I try:
public async Task<Product> TryGetProduct(int id, out var product)
then I get:
Async methods cannot have ref or out parameters
The signature would be
public interface IRepository
{
bool TryGetProduct(int id, out Product product);
}
And in the implementation, If your database has a record for the Id value, you will set that to the Product object and return true, else false.
Something like this (not tested)
public class Repository: IRepository
{
YourDbContext yourDbContext;
// to do : Initialize yourDbContext via constructor injection
public bool TryGetProduct(int id, out Product product)
{
var p = yourDbContext.Products.FirstOrDefault(a => a.Id == id);
if (p != null)
{
product = p;
return true;
}
product = null;
return false;
}
}
To keep the async task functionality you can do this.
public interface IRepository
{
Task<bool> TryGetProduct(int id, out Product product);
}
public class Repository : IRepository
{
public Task<bool> TryGetProduct(int id, out Product product)
{
product = _db.Products.SingleOrDefault(x => x.Id == id);
return Task.FromResult(product != null);
}
}
Then in the controller.
[HttpGet("{id}")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<ActionResult<Product>> GetById(int id)
{
// await
if (!await _repository.TryGetProduct(id, out var product))
{
return NotFound();
}
return Ok(product);
}

Aspnet Core Identity Role Update Error

I am trying update exist role but i am getting error.
Code
private readonly RoleManager<IdentityRole> _roleManager;
public EditModel(RoleManager<IdentityRole> roleManager)
{
_roleManager = roleManager;
}
[BindProperty]
public IdentityRole IdentityRole { get; set; }
public async Task<IActionResult> OnGetAsync(string id)
{
if (id == null)
{
return NotFound();
}
IdentityRole = await _roleManager.FindByIdAsync(id);
if (IdentityRole == null)
{
return NotFound();
}
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
try
{
await _roleManager.UpdateAsync(IdentityRole);//Error is occuring here.
}
catch (DbUpdateConcurrencyException)
{
}
return RedirectToPage("./Index");
}
Error
InvalidOperationException: The instance of entity type 'IdentityRole' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.
Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap.Add(TKey key, InternalEntityEntry entry)
I changed codes like that and its work but that is a weird and when i change name that changes automatic IdentityRole normalized name column.
var role = await _roleManager.FindByIdAsync(IdentityRole.Id);
role.Name = IdentityRole.Name;
await _roleManager.UpdateAsync(role);
I found there enter link description here

ASP.NET Core policy base Authorize with RequireUser with string array

I am creating policy base authorization and would like to allow multiple users in one policy to access webpage.
I created policy like shown below in start up file. Question, How can I use multiple usernames in one policy? I looked at the method for.RequireUserName, it is only accepting string username.
Policy name AdminServiceAccount is mostly I am interested in to add multiple users. If I use param .RequireUserName("DOMAIN\\USER1,DOMAIN\\USER2") will it work? I don't think so, but wanted to check if there is an alternative way.
services.AddAuthorization(
option =>
{
option.AddPolicy("Admin", policy => policy.RequireRole("Domain\\GroupName"));
option.AddPolicy("SuperAdminUser", policy => policy.RequireUserName("DOMAIN\\SuperAdminUser"));
option.AddPolicy("AdminServiceAccount", policy => policy.RequireUserName("DOMAIN\\USER1"));
}
);
UPDATE 1:
UPDATE 2:
So in my Controller, I added [Authorize(Policy = "UserNamesPolicy")] as show below:
[Authorize(Policy = "UserNamesPolicy")]
public class ServersController : Controller
{
private readonly ServerMatrixDbContext _context;
public ServersController(ServerMatrixDbContext context)
{
_context = context;
}
// GET: Servers
public async Task<IActionResult> Index()
{
// Some code here
return View();
}
}
Here is my startup file:
services.AddAuthorization(
option =>
{
option.AddPolicy("UserNamesPolicy",
policy => policy.Requirements.Add(new UserNamesRequirement("DOMAIN\\USER1", "DOMAIN\\USER2"))
);
}
);
services.AddSingleton<IAuthorizationHandler, UserNamesRequirement();
For .AddSingleTon in startup file I get below error:
Here is the handler class:
public class UserNamesHandler : AuthorizationHandler<UserNamesRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserNamesRequirement requirement)
{
var userName = context.User.FindFirst(ClaimTypes.NameIdentifier).Value;
if (requirement.Users.ToList().Contains(userName))
context.Succeed(requirement);
return Task.FromResult(0);
}
}
Here is is the UserNamesRequirement class:
public class UserNamesRequirement : IAuthorizationRequirement
{
public UserNamesRequirement(params string[] UserNames)
{
Users = UserNames;
}
public string[] Users { get; set; }
}
UPDATE 3: SOLVED!!!!
Here are few changes that were added from update 2:
In UserNameshandler class changed var userName to get values from context.User.Identity.Name;
public class UserNamesHandler : AuthorizationHandler<UserNamesRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserNamesRequirement requirement)
{
// var userName = context.User.FindFirst(ClaimTypes.NameIdentifier).Value;
var userName = context.User.Identity.Name;
if (requirement.Users.ToList().Contains(userName))
context.Succeed(requirement);
return Task.FromResult(0);
}
}
In StartUp class fixed from services.AddSingleton<IAuthorizationHandler, UserNamesRequirement>(); to services.AddSingleton<IAuthorizationHandler,UserNamesHandler>();
Thanks to Gevory. :)
public class UserNamesHandler : AuthorizationHandler<UserNamesRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserNamesRequirement requirement)
{
var userName = context.User.Identity.Name;
if(requirement.UserNames.ToList().Contains(userName))
context.Succeed(requirement);
return Task.CompletedTask; // if it does not compile use Task.FromResult(0);
}
}
public class UserNamesRequirement : IAuthorizationRequirement
{
public UserNamesRequirement(params string[] userNames)
{
UserNames = userNames;
}
public string[] UserNames { get; set; }
}
in startup.cs add the following
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthorization(options =>
{
options.AddPolicy("UserNamesPolicy",
policy => policy.Requirements.Add(new UserNamesRequirement("ggg","dsds")));
});
services.AddSingleton<IAuthorizationHandler, UserNamesHandler>()
}
Just for anyone coming to this who wants a different approach, I battled with trying to get the UserNamesHandler to register properly, to no avail. So I solved this in a different way:
static readonly string[] myUserList = { "user1", "user2", "user3" };
options.AddPolicy( "MyNameListPolicy",
policy => policy.RequireAssertion(
context => myUserList.Contains( context.User.Identity.Name ) ) );
This worked fine for me.

Many to many not saving when updating/editing entity

I'm having a problem with my [HttpPost] edit method in my controller, it is not saving the changes made to a userrole, it is strange because the create method is working it is using the same helper methods, this is my code:
viewmodel:
public class UserViewModel
{
public User User { get; set; }
public virtual ICollection<AssignedUserRole> UserRoles { get; set; }
public virtual List<Company> Companies { get; set; }
}
Controller:
[HttpPost]
public ActionResult Edit(UserViewModel userViewModel)
{
if (ModelState.IsValid)
{
var user = userViewModel.User;
user.UserRoles.Clear();
AddOrUpdateRoles(user, userViewModel.UserRoles);
context.Entry(user).State = EntityState.Modified;
context.SaveChanges();
return RedirectToAction("Index");
}
return View(userViewModel);
}
Helper Method
private void AddOrUpdateRoles(User user, ICollection<AssignedUserRole> assignedUserRoles)
{
foreach (var assignedRole in assignedUserRoles)
{
if (assignedRole.Assigned)
{
var userRole = new UserRole { Id = assignedRole.UserRoleId };
context.UserRoles.Attach(userRole);
user.UserRoles.Add(userRole);
}
}
}
everything in the User object is being updated except for the userrole, I can't find the problem as I am debugging and doing a step through and I can see that the user has the correct/updated roles assigned.
I was able to solve this problem by making the following changes:
private void AddOrUpdateRoles(User user, ICollection<AssignedUserRole> assignedUserRoles)
{
foreach (var assignedRole in assignedUserRoles)
{
if (assignedRole.Assigned)
{
var userRole = context.UserRoles.Find(assignedRole.UserRoleId);
user.UserRoles.Add(userRole);
}
}
}
[HttpPost]
public ActionResult Edit(UserViewModel userViewModel)
{
if (ModelState.IsValid)
{
var user = userViewModel.User;
context.Entry(user).State = EntityState.Modified;
context.Entry(user).Collection(u => u.UserRoles).Load();
user.UserRoles.Clear();
AddOrUpdateRoles(user, userViewModel.UserRoles);
context.SaveChanges();
return RedirectToAction("Index");
}
return View(userViewModel);
}
I had to "Load" the user's userroles otherwise the clearing was doing nothing.

Resources