public async Task <IActionResult> CreateUser([FromBody] UserRequestVM requestVm, CancellationToken ct)
        {
            var             password = Constant.AlphanumericCaps.RandomPassword(Constant.DefaultPasswordLength);
            ApplicationUser user     = requestVm.ConvertAdd();
            var             result   = await UserApplication.CreateAsync(user, password, false, base.UseDefaultLanguage);//TODO: need to modified zeeshan

            // Checking if user was created
            if (result.Succeeded)
            {
                await UserApplication.AddToRolesAsync(user, requestVm.Roles, ct);

                Employee employee = new Employee
                {
                    UserTypeId = requestVm.UserTypeId,
                    EmployeeId = user.UserId
                };

                await EmployeeApplication.Add(employee);

                return(Ok(user.UserId));
            }
            else
            {
                return(BadRequest(user.UserId));
            }
        }
        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);
        }
示例#4
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 static ApplicationUser ConvertAdd(this UserRequestVM vm)
        {
            var model = new ApplicationUser
            {
                FirstName            = vm.FirstName,
                LastName             = vm.LastName,
                Email                = vm.Email,
                EmailConfirmed       = vm.EmailConfirmed,
                UserName             = vm.Email,
                PhoneNumber          = vm.PhoneNumber,
                Active               = vm.Active,
                CreatedById          = vm.CurrentUserId,
                UserTypeId           = vm.UserTypeId,
                UserRoles            = vm.Roles,
                IdentificationNumber = vm.IdentificationNumber ?? ""
            };

            return(model);
        }
        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"));
        }