Beispiel #1
0
        public async Task <IActionResult> Edit(long id, [Bind("FuncionarioId,Email,Password,Name,DataNascimento,Endereco,Telefone,CPF, Gerente, Sexo, LojaId")] Models.AccountViewModels.RegisterViewModel funcionario)
        {
            if (id != funcionario.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var user = new Models.ApplicationUser {
                        UserName = funcionario.Name, Email = funcionario.Email
                    };
                    await _userManager.UpdateAsync(user);
                }
                catch (System.Exception)
                {
                    if (!FuncionarioExists(funcionario.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["LojaId"] = new SelectList(_repositoryLoja.GetAll(), "Id", "Nome", funcionario.LojaId);
            return(View(funcionario));
        }
Beispiel #2
0
        public async Task <IActionResult> Create(Models.AccountViewModels.RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName    = model.Email,
                    Email       = model.Email,
                    Name        = model.Name,
                    Surname     = model.Surname,
                    JobTitle    = model.JobTitle,
                    Stations    = model.Stations,
                    PhoneNumber = model.Phone
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);

                    await _userManager.AddToRoleAsync(user, "Operations");

                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #3
0
        public async Task <IActionResult> GetRegister([FromBody] Models.AccountViewModels.RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, FullName = model.FullName
                };

                ///user.IsSuperAdmin = true;
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    return(Json(new { success = true, message = "OK" }));
                }
                else
                {
                    return(Json(new { success = true, message = "Can t create user." }));
                }
            }
            else
            {
                return(Json(new { success = false, message = "Invalid data." }));
            }
        }
Beispiel #4
0
        public async Task <IActionResult> SignUpAsync([FromForm] Models.AccountViewModels.RegisterViewModel regModel)
        {
            if (!ModelState.IsValid)
            {
                return(new StatusCodeResult(StatusCodes.Status400BadRequest));
            }
            var userInfo = new SparkTodo.Models.UserAccount()
            {
                UserName       = regModel.Email,
                Email          = regModel.Email,
                EmailConfirmed = true,//ĬÈϲ»ÐèÒªÑéÖ¤ÓÊÏ䣬עÊÍÒÔÆôÓÃ
                CreatedTime    = DateTime.Now
            };
            var result       = new Models.JsonResponseModel <JWT.TokenEntity>();
            var signupResult = await _userManager.CreateAsync(userInfo, regModel.Password);

            if (signupResult.Succeeded)
            {
                _logger.LogInformation(3, "User created a new account");
                var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_apiSetting.Value.SecretKey));
                var options    = new JWT.TokenOptions
                {
                    Audience           = "SparkTodoAudience",
                    Issuer             = "SparkTodo",
                    SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
                };
                var token = new TokenProvider(options).GenerateToken(HttpContext, userInfo.Email);
                userInfo = await _userRepository.FetchAsync(u => u.Email == regModel.Email);

                var userToken = new UserTokenEntity
                {
                    AccessToken = token.AccessToken,
                    ExpiresIn   = token.ExpiresIn,
                    UserEmail   = userInfo.Email,
                    UserId      = userInfo.UserId,
                    UserName    = userInfo.UserName
                };
                result = new Models.JsonResponseModel <JWT.TokenEntity> {
                    Data = userToken, Msg = "×¢²á³É¹¦", Status = Models.JsonResponseStatus.Success
                };
            }
            else
            {
                result = new Models.JsonResponseModel <JWT.TokenEntity> {
                    Data = null, Msg = "sign up failed," + String.Join(",", signupResult.Errors.Select(e => e.Description).ToArray()), Status = Models.JsonResponseStatus.ProcessFail
                };
            }
            return(Json(result));
        }
Beispiel #5
0
        public async Task <IActionResult> SignUpAsync([FromBody] Models.AccountViewModels.RegisterViewModel regModel)
        {
            if (!ModelState.IsValid)
            {
                return(new StatusCodeResult(StatusCodes.Status400BadRequest));
            }
            var userInfo = new SparkTodo.Models.UserAccount()
            {
                UserName       = regModel.Email,
                Email          = regModel.Email,
                EmailConfirmed = true,
                CreatedTime    = DateTime.Now
            };
            var result       = new Models.JsonResponseModel <JWT.TokenEntity>();
            var signupResult = await _userManager.CreateAsync(userInfo, regModel.Password);

            if (signupResult.Succeeded)
            {
                _logger.LogInformation(3, "User created a new account");
                userInfo = await _userRepository.FetchAsync(u => u.Email == regModel.Email);

                var claims = new Claim[]
                {
                    new Claim(JwtRegisteredClaimNames.Jti, ObjectIdGenerator.Instance.NewId()),
                    new Claim(JwtRegisteredClaimNames.Sub, userInfo.Email),
                    new Claim(ClaimTypes.Name, userInfo.UserName),
                };
                var token = _tokenGenerator.GenerateToken(claims);

                var userToken = new UserTokenEntity
                {
                    AccessToken = token.AccessToken,
                    ExpiresIn   = token.ExpiresIn,
                    UserEmail   = userInfo.Email,
                    UserId      = userInfo.Id,
                    UserName    = userInfo.UserName
                };
                result = new Models.JsonResponseModel <JWT.TokenEntity> {
                    Data = userToken, Msg = "", Status = Models.JsonResponseStatus.Success
                };
            }
            else
            {
                result = new Models.JsonResponseModel <JWT.TokenEntity> {
                    Data = null, Msg = "sign up failed," + string.Join(",", signupResult.Errors.Select(e => e.Description).ToArray()), Status = Models.JsonResponseStatus.ProcessFail
                };
            }
            return(Json(result));
        }
Beispiel #6
0
        public async Task <IActionResult> Create([Bind("FuncionarioId,Email,Password,Name,DataNascimento,Endereco,Telefone,CPF, Gerente, Sexo, LojaId")] Models.AccountViewModels.RegisterViewModel funcionario)
        {
            if (ModelState.IsValid)
            {
                var user = new Models.ApplicationUser {
                    UserName = funcionario.Name, Email = funcionario.Email
                };
                await _userManager.CreateAsync(user, funcionario.Email);

                return(RedirectToAction(nameof(Index)));
            }

            ViewData["LojaId"] = new SelectList(_repositoryLoja.GetAll(), "Id", "Nome", funcionario.LojaId);
            return(View(funcionario));
        }
Beispiel #7
0
        public async Task <IActionResult> SignUpAsync([FromBody] Models.AccountViewModels.RegisterViewModel regModel)
        {
            var userInfo = new SparkTodo.Models.UserAccount()
            {
                UserName       = regModel.Email,
                Email          = regModel.Email,
                EmailConfirmed = true,
                CreatedTime    = DateTime.UtcNow
            };
            JsonResponseModel <TokenEntity> result;
            var signUpResult = await _userManager.CreateAsync(userInfo, regModel.Password);

            if (signUpResult.Succeeded)
            {
                userInfo = await _userRepository.FetchAsync(u => u.Email == regModel.Email);

                var claims = new[]
                {
                    new Claim(JwtRegisteredClaimNames.Jti, GuidIdGenerator.Instance.NewId()),
                    new Claim(JwtRegisteredClaimNames.Sub, userInfo.Email),
                    new Claim(JwtRegisteredClaimNames.NameId, userInfo.Id.ToString()),
                };
                var token = _tokenGenerator.GenerateToken(claims);

                var userToken = new UserTokenEntity
                {
                    AccessToken = token.AccessToken,
                    ExpiresIn   = token.ExpiresIn,
                    UserEmail   = userInfo.Email,
                    UserId      = userInfo.Id,
                    UserName    = userInfo.UserName
                };
                result = new Models.JsonResponseModel <JWT.TokenEntity> {
                    Data = userToken, Msg = "", Status = Models.JsonResponseStatus.Success
                };
            }
            else
            {
                result = new Models.JsonResponseModel <JWT.TokenEntity> {
                    Msg = "sign up failed," + string.Join(",", signUpResult.Errors.Select(e => e.Description).ToArray()), Status = Models.JsonResponseStatus.ProcessFail
                };
            }
            return(Ok(result));
        }
        public async Task <IActionResult> Register(Models.AccountViewModels.RegisterViewModel registerViewModel, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new User
                {
                    UserName = registerViewModel.Login
                };
                if (registerViewModel.Password == registerViewModel.ConfirmPassword)
                {
                    user.Password     = registerViewModel.Password;
                    user.PasswordHash = _userManager.PasswordHasher.HashPassword(user, registerViewModel.Password);
                    var result = await _userManager.CreateAsync(user);

                    if (result.Succeeded)
                    {
                        _logger.LogInformation("User has been created");
                        var role = await _roleManager.FindByNameAsync("User");

                        result = await _userManager.AddToRoleAsync(user, role.Name);

                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(RedirectToAction("Index", "Home"));
                    }
                    else if (result.Errors.Count() > 0)
                    {
                        foreach (var item in result.Errors)
                        {
                            ModelState.AddModelError(string.Empty, item.Description);
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Hasła się nie zgadzają!");
                }
            }
            return(View(registerViewModel));
        }
        /// <summary>
        /// 全部用户
        /// </summary>
        /// <returns></returns>
        public IActionResult UserList(Models.AccountViewModels.RegisterViewModel model, int pageindex = 1, int isallowmatch = 1)
        {
            List <ApplicationUser> list = this.dbContext.Users.Where(item => (model.UserName == null || item.UserName == model.UserName) && (model.Email == null || item.Email == model.Email) && item.isallowmatch == isallowmatch).OrderByDescending(item => item.groupid).Skip(50 * (pageindex - 1)).Take(50).ToList();

            return(View(list));
        }