public async Task <ApplicationUser> GetUserByEmail([FromQuery] UserRequestVM requestVM)
        {
            ApplicationUser request = requestVM.Convert();
            var             user    = await UserApplication.GetUserbyEmail(request.Email);

            return(user);
        }
        public async Task <UserResponseVM> GetUserById([FromQuery] UserRequestVM requestVM)
        {
            ApplicationUser request = requestVM.Convert();

            ApplicationUser response = await this.UserApplication.GetbyUserID(request);

            UserResponseVM responseVm = response.Convert(base.UseDefaultLanguage);

            return(responseVm);
        }
Esempio n. 3
0
        public async Task <List <ClaimListResponseVM> > GetListByUser([FromQuery] UserRequestVM requestVM)
        {
            ApplicationUser request = requestVM.Convert();

            List <ApplicationClaim> response = await this.ClaimApplication.GetListByUser(request);

            List <ClaimListResponseVM> responseVM = response.ConvertList(base.UseDefaultLanguage);

            return(responseVM);
        }
        public async Task <IActionResult> ResetPassword(UserRequestVM requestVM, CancellationToken ct)
        {
            ApplicationUser applicationUser = requestVM.Convert();
            var             user            = await UserApplication.GetbyUserID(applicationUser);

            if (user == null)
            {
                // Don't reveal that the user does not exist
                //return RedirectToAction(nameof(ResetPasswordConfirmation));
            }
            var result = await UserApplication.ResetPasswordAsync(user, requestVM.Code, requestVM.Password);

            if (result.Succeeded)
            {
                return(Ok());
            }
            return(BadRequest("Not updated"));
        }
        public async Task <IActionResult> Update([FromBody] UserRequestVM requestVm, CancellationToken ct)
        {
            ApplicationUser user = requestVm.Convert();

            var result = await UserApplication.Update(user, ct);

            // Checking if user was created
            if (result.Succeeded)
            {
                // bool isExisted;
                await UserApplication.RemoveExistingRolesForUser(user, ct);

                foreach (var role in requestVm.Roles)
                {
                    await UserApplication.InsertRole(user, role.RoleName, ct);
                }

                return(Ok());
            }
            else
            {
                return(BadRequest("User Not Updated"));
            }
        }
        public async Task <IActionResult> Login([FromBody] UserRequestVM paramUser, CancellationToken ct)
        {
            var result = await UserApplication.PasswordSignInAsync(paramUser.Email, paramUser.Password, false, lockoutOnFailure : false);

            if (result.Succeeded)
            {
                ApplicationUser request = paramUser.Convert();

                ApplicationUser currentUser = await this.UserApplication.GetUserWithRolesByEmail(request);

                if (base.HeaderValue?.ApplicationKey != this.AdminPortal && currentUser != null && !currentUser.UserTypeName.Equals(UserTypeEnum.Administrator.ToString()) &&
                    !currentUser.UserTypeCode.Equals(this.AdminTypeCode))
                {
                    //List<ApplicationClaim> UserClaims = await this.ClaimApplication.GetListByUser(UserDetails); // commented getUserClaims for claim-based-auth

                    var Claims = new ClaimsIdentity(new Claim[]
                    {
                        new Claim(JwtRegisteredClaimNames.Sub, paramUser.Email.ToString()),
                        new Claim(UserId, currentUser.UserId.ToString())
                    });

                    //adding role claims
                    foreach (var role in currentUser.Roles)
                    {
                        Claims.AddClaim(new Claim(ClaimsIdentity.DefaultRoleClaimType, role.RoleNameCode));
                    }


                    var tokenHandler    = new JwtSecurityTokenHandler();
                    var encryptionkey   = Configuration["Jwt:Encryptionkey"];
                    var key             = Encoding.ASCII.GetBytes(encryptionkey);
                    var tokenDescriptor = new SecurityTokenDescriptor
                    {
                        Issuer  = Configuration["Jwt:Issuer"],
                        Subject = Claims,

                        Expires            = DateTime.UtcNow.AddMinutes(Convert.ToDouble(Configuration["Jwt:ExpiryTimeInMinutes"])),
                        SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                    };

                    SecurityToken token       = tokenHandler.CreateToken(tokenDescriptor);
                    var           tokenString = tokenHandler.WriteToken(token);


                    return(Ok(new
                    {
                        token = tokenString
                    }));
                }
                else if ((base.HeaderValue?.ApplicationKey == this.AdminPortal && currentUser != null && currentUser.UserTypeName.Equals(UserTypeEnum.Administrator.ToString()) &&
                          currentUser.UserTypeCode.Equals(this.AdminTypeCode)))
                {
                    List <ApplicationClaim> UserClaims = await this.ClaimApplication.GetListByUser(currentUser); // commented getUserClaims for claim-based-auth

                    var Claims = new ClaimsIdentity(new Claim[]
                    {
                        new Claim(JwtRegisteredClaimNames.Sub, paramUser.Email.ToString()),
                        new Claim(UserId, currentUser.UserId.ToString())
                    });

                    foreach (var item in UserClaims)
                    {
                        Claims.AddClaim(new Claim(item.ClaimCode, string.Empty));
                    }

                    var tokenHandler    = new JwtSecurityTokenHandler();
                    var encryptionkey   = Configuration["Jwt:Encryptionkey"];
                    var key             = Encoding.ASCII.GetBytes(encryptionkey);
                    var tokenDescriptor = new SecurityTokenDescriptor
                    {
                        Issuer  = Configuration["Jwt:Issuer"],
                        Subject = Claims,

                        Expires            = DateTime.UtcNow.AddMinutes(Convert.ToDouble(Configuration["Jwt:ExpiryTimeInMinutes"])),
                        SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
                    };

                    SecurityToken token       = tokenHandler.CreateToken(tokenDescriptor);
                    var           tokenString = tokenHandler.WriteToken(token);

                    return(Ok(new
                    {
                        token = tokenString
                    }));
                }
            }
            return(BadRequest("Wrong Username or password"));
        }