From 82d270a66b8ffca28e321f29b2eb90b2412ac9a7 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sat, 8 May 2021 18:10:08 +0300 Subject: Implemented authorization; Replaced Role with IdentityRole; Renamed UserController to AccountController, updated links --- ExamTemplate/Services/UserService.cs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'ExamTemplate/Services/UserService.cs') diff --git a/ExamTemplate/Services/UserService.cs b/ExamTemplate/Services/UserService.cs index 90a4bf4..e78443a 100644 --- a/ExamTemplate/Services/UserService.cs +++ b/ExamTemplate/Services/UserService.cs @@ -1,4 +1,5 @@ -using System.Security.Claims; +using System; +using System.Security.Claims; using System.Threading.Tasks; using AutoMapper; using ExamTemplate.Data; @@ -15,9 +16,9 @@ namespace ExamTemplate.Services private readonly TemplateContext _context; private readonly SignInManager _signInManager; private readonly UserManager _userManager; - private readonly RoleManager _roleManager; + private readonly RoleManager> _roleManager; - public UserService(IMapper autoMapper, TemplateContext templateContext, SignInManager signInManager, UserManager userManager, RoleManager roleManager) + public UserService(IMapper autoMapper, TemplateContext templateContext, SignInManager signInManager, UserManager userManager, RoleManager> roleManager) { this._autoMapper = autoMapper; this._context = templateContext; @@ -32,14 +33,9 @@ namespace ExamTemplate.Services user.PasswordHash = this._userManager.PasswordHasher.HashPassword(user, registerUserServiceModel.Password); IdentityResult userCreateResult = await this._userManager.CreateAsync(user); - - // Many to many relationships with Roles can cause problems, - // that's why I add the Role to the User and add the User to the Role IdentityResult addRoleResult = await this._userManager.AddToRoleAsync(user, "User"); - user.Roles.Add(await this._roleManager.FindByNameAsync("User")); - bool roleAddedSuccessfuly = await this._context.SaveChangesAsync() >= 1; - return userCreateResult.Succeeded && addRoleResult.Succeeded && roleAddedSuccessfuly; + return userCreateResult.Succeeded && addRoleResult.Succeeded; } public async Task LoginUserAsync(LoginUserServiceModel loginUserServiceModel) @@ -57,7 +53,6 @@ namespace ExamTemplate.Services public async Task GetUserByUsernameAsync(string username) { User user = await this._userManager.Users - .Include(x => x.Roles) .FirstOrDefaultAsync(x => x.UserName == username); return this._autoMapper.Map(user); -- cgit v1.2.3