예제 #1
0
        public async Task <IActionResult> Index(LoginViewModel viewModel, string returnUrl = "")
        {
            if (ModelState.IsValid)
            {
                string sql     = $"select * from Manager where Mobile = @mobile";
                var    channel = _repository.Find(sql, new { @mobile = viewModel.UserName });
                if (channel == null)
                {
                    ModelState.AddModelError(viewModel.UserName, "用户名或密码错误");
                    return(View(viewModel));
                }
                else
                {
                    if (channel.Status == 0)
                    {
                        var encrypt        = channel.Encrypt;
                        var encodepassword = Cryptographer.EncodePassword(viewModel.Pwd, 1, encrypt);
                        if (channel.Password.Equals(encodepassword))
                        {
                            var claimsIdentity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
                            claimsIdentity.AddClaims(new List <Claim>()
                            {
                                new Claim(ClaimTypes.Sid, channel.Id.ToString()),
                                new Claim(ClaimTypes.Name, channel.Mobile),
                                new Claim(ClaimTypes.Role, "Manager"),
                                new Claim(ClaimTypes.Uri, channel.PicUrl ?? ""),
                                new Claim("UserName", channel.Name)
                            });
                            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), new AuthenticationProperties()
                            {
                                IsPersistent = true,
                                ExpiresUtc   = DateTimeOffset.UtcNow.AddMinutes(60),
                                AllowRefresh = true
                            });

                            if (!string.IsNullOrEmpty(returnUrl))
                            {
                                return(Redirect(returnUrl));
                            }
                            CommonManager.CacheObj.RemoveCache("nav" + channel.Id.ToString());
                            return(Redirect("/Main/Index"));
                        }
                        else
                        {
                            ModelState.AddModelError(viewModel.UserName, "用户名或密码错误");
                            return(View(viewModel));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError(viewModel.UserName, "用户名或密码错误");
                        return(View(viewModel));
                    }
                }
            }
            return(View(viewModel));
        }
예제 #2
0
        public Users Login(string userName, string pwd)
        {
            var users = db.Queryable <Users>().Where(s => s.Account.Equals(userName));

            if (users.Count() > 0)
            {
                var user           = users.First();
                var encrypt        = user.Encrypt;
                var encodepassword = Cryptographer.EncodePassword(pwd, 1, user.Encrypt);
                if (user.Password == encodepassword)
                {
                    return(user);
                }
            }
            return(default(Users));
        }
예제 #3
0
 public Task <bool> Handle(UsersInsertCommand request, CancellationToken cancellationToken)
 {
     if (request.IsValid())
     {
         try
         {
             using (var connection = _usersRepository.GetFirstConnection())
             {
                 if (connection.State == System.Data.ConnectionState.Closed)
                 {
                     connection.Open();
                 }
                 var transaction = connection.BeginTransaction(System.Data.IsolationLevel.ReadUncommitted);
                 var model       = _usersRepository.Find("select * from Users where mobile = @mobile", new { @mobile = request.Users.Mobile }, transaction);
                 if (model != null)
                 {
                     ///事件收集
                     _bus.RaiseEvent(new DomainNotification("", "该用户已存在!"));
                 }
                 else
                 {
                     var passwordSalt = Cryptographer.CreateSalt();
                     var password     = Cryptographer.EncodePassword(request.Users.Password, 1, passwordSalt);
                     var result       = _usersRepository.Insert(new UsersEntities(request.Users.Id, request.Users.UserName, password, request.Users.Name, request.Users.Email, DateTime.Now, request.Users.Mobile, (int)CommonState.正常, request.Users.PicUrl, passwordSalt), transaction);
                     transaction.Commit();
                     if (result != null)
                     {
                         return(Task.FromResult(true));
                     }
                     return(Task.FromResult(false));
                 }
             }
         }
         catch (Exception ex)
         {
             throw;
         }
     }
     else
     {
         //验证不通过
         NotifyValidationErrors(request);
     }
     return(Task.FromResult(false));
 }
예제 #4
0
 public IActionResult Reset(ResetPwd reset)
 {
     if (ModelState.IsValid)
     {
         var model = _repositoryEF.Find(reset.Id);
         if (model == null)
         {
             return(Json(new { Data = false }));
         }
         else
         {
             model.Encrypt  = Cryptographer.CreateSalt();
             model.Password = Cryptographer.EncodePassword(reset.Pwd, 1, model.Encrypt);
             _repositoryEF.Update(model);
             _unitOfWork.Commit();
             return(Json(new { Data = true }));
         }
     }
     return(Json(new { Data = false }));
 }
예제 #5
0
        public async Task <IActionResult> Add(ManagerViewModel entities)
        {
            if (ModelState.IsValid)
            {
                var isTrue = _repositoryEF.Count(s => s.Account.Equals(entities.Account) || s.Mobile.Equals(entities.Mobile)) > 0;
                if (isTrue)
                {
                    ModelState.AddModelError("Account", "该账户或联系电话已存在");
                    return(View(entities));
                }
                var entity = _mapper.Map <ManagerEntities>(entities);
                entity.Status     = (int)CommonState.正常;
                entity.CreateTime = DateTime.Now;
                entity.Encrypt    = Cryptographer.CreateSalt();
                entity.Password   = Cryptographer.EncodePassword("123qwe", 1, entity.Encrypt);
                await _repositoryEF.InsertAsync(entity);

                _unitOfWork.Commit();
                return(Json(new { d = true }));
            }
            return(Json(new { d = false }));
        }
예제 #6
0
        public async Task <IActionResult> Login(LoginInputModel model, string button)
        {
            // 检查我们是否在授权请求的上下文中
            var context = await _interaction.GetAuthorizationContextAsync(model.ReturnUrl);

            // 用户点击了“取消”按钮
            if (button != "login")
            {
                if (context != null)
                {
                    await _interaction.GrantConsentAsync(context, ConsentResponse.Denied);

                    if (await _clientStore.IsPkceClientAsync(context.ClientId))
                    {
                        return(View("Redirect", new RedirectViewModel {
                            RedirectUrl = model.ReturnUrl
                        }));
                    }
                    return(Redirect("~/"));
                }
                else
                {
                    return(Redirect("~/"));
                }
            }
            if (ModelState.IsValid)
            {
                if (model.UserType == "1")
                {
                    var user = _repository.Find("select * from Users where Mobile = @mobile and Status = 0", new { mobile = model.Username });
                    if (user != null)
                    {
                        var encodepassword = Cryptographer.EncodePassword(model.Password, 1, user.Encrypt);
                        if (encodepassword.Equals(user.Password))
                        {
                            await _events.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id.ToString(), user.Mobile));

                            AuthenticationProperties props = null;
                            if (AccountOptions.AllowRememberLogin && model.RememberLogin)
                            {
                                props = new AuthenticationProperties
                                {
                                    IsPersistent = true,
                                    ExpiresUtc   = DateTimeOffset.UtcNow.Add(AccountOptions.RememberMeLoginDuration)
                                };
                            }
                            await HttpContext.SignInAsync(user.Id.ToString(), user.Mobile, props);

                            if (_interaction.IsValidReturnUrl(model.ReturnUrl) || Url.IsLocalUrl(model.ReturnUrl))
                            {
                                return(Redirect(model.ReturnUrl));
                            }
                            return(Redirect("~/"));
                        }
                    }
                    await _events.RaiseAsync(new UserLoginFailureEvent(model.Username, "无效用户名或密码"));

                    ModelState.AddModelError("", AccountOptions.InvalidCredentialsErrorMessage);
                }
                else
                {
                    var user = _repository.Find("select * from Consultant where Mobile = @mobile and Status = 0", new { mobile = model.Username });
                }
            }
            return(View());
        }