diff options
| author | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-05-08 18:10:08 +0300 |
|---|---|---|
| committer | Syndamia <kamen.d.mladenov@protonmail.com> | 2021-05-08 18:10:08 +0300 |
| commit | 82d270a66b8ffca28e321f29b2eb90b2412ac9a7 (patch) | |
| tree | bd7e985592a0d8d5ec31b590b3c52d403e17e140 /ExamTemplate/Services | |
| parent | a1e46b76a1299e35b1ac8cae69e77c66d74224a6 (diff) | |
| download | it-kariera-exam-template-82d270a66b8ffca28e321f29b2eb90b2412ac9a7.tar it-kariera-exam-template-82d270a66b8ffca28e321f29b2eb90b2412ac9a7.tar.gz it-kariera-exam-template-82d270a66b8ffca28e321f29b2eb90b2412ac9a7.zip | |
Implemented authorization; Replaced Role with IdentityRole<Guid>; Renamed UserController to AccountController, updated links
Diffstat (limited to 'ExamTemplate/Services')
| -rw-r--r-- | ExamTemplate/Services/UserService.cs | 15 |
1 files changed, 5 insertions, 10 deletions
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<User> _signInManager;
private readonly UserManager<User> _userManager;
- private readonly RoleManager<Role> _roleManager;
+ private readonly RoleManager<IdentityRole<Guid>> _roleManager;
- public UserService(IMapper autoMapper, TemplateContext templateContext, SignInManager<User> signInManager, UserManager<User> userManager, RoleManager<Role> roleManager)
+ public UserService(IMapper autoMapper, TemplateContext templateContext, SignInManager<User> signInManager, UserManager<User> userManager, RoleManager<IdentityRole<Guid>> 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<bool> LoginUserAsync(LoginUserServiceModel loginUserServiceModel)
@@ -57,7 +53,6 @@ namespace ExamTemplate.Services public async Task<UserServiceModel> GetUserByUsernameAsync(string username)
{
User user = await this._userManager.Users
- .Include(x => x.Roles)
.FirstOrDefaultAsync(x => x.UserName == username);
return this._autoMapper.Map<UserServiceModel>(user);
|
