Esempio n. 1
0
        public IActionResult LoginConfirm([FromForm] LoginViewModel login)
        {
            var user = _appService.Login(login.UserName, login.Password);

            if (user == null)
            {
                return(RedirectToAction("Login"));
            }
            return(RedirectToAction("index", "Home"));
        }
Esempio n. 2
0
        public string LoginUser(Login_AdminInput input)
        {
            var check = Session["check"];
            var ret   = string.Empty;

            if (input.verifyCode != check.ToString())
            {
                ret = "验证码错误";//登录失败。验证码错误
            }
            else
            {
                var loginState = _adminAppservice.Login(input);


                if (loginState.state == 0)
                {
                    ret = "登录失败";//登录失败
                }
                else
                {
                    ret = "登录成功";//登录成功

                    HttpContext.Session["UserName"] = loginState.userInfo.UserName;
                    HttpContext.Session.Timeout     = 6000;//以分种为单位

                    //HttpCookie cookie = new HttpCookie("userId", Convert.ToString(loginState.userInfo.Id));
                    //cookie.Expires = DateTime.Now.AddMinutes(30);//30分钟的co0kie
                    //System.Web.HttpContext.Current.Response.AppendCookie(cookie); //写入Cookie
                    //HttpCookie cookie1 = new HttpCookie("userName", loginState.userInfo.UserName);
                    //cookie1.Expires = DateTime.Now.AddMinutes(30);//30分钟的co0kie
                    //System.Web.HttpContext.Current.Response.AppendCookie(cookie1); //写入Cookie
                }
            }


            return(ret);
        }
Esempio n. 3
0
        public async Task <IActionResult> OnPostAsync()
        {
            ErrorMessage = "";
            if (!ModelState.IsValid)
            {
                ErrorMessage = ModelState.Where(e => e.Value.Errors.Count > 0).Select(e => e.Value.Errors.First().ErrorMessage).First();
                var adminCount = await _adminAppService.GetCount();

                HasSetAdmin = adminCount > 0;
                return(Page());
            }

            var result = await _adminAppService.Login(LoginInput);

            if (!result.IsSuccess)
            {
                ErrorMessage = result.Message;
                var adminCount = await _adminAppService.GetCount();

                HasSetAdmin = adminCount > 0;
                return(Page());
            }
            else
            {
                return(RedirectToPage("Index"));
            }

            /*
             * AdminEntity admin = null;
             * if (_db.Admins.Count() == 0)
             * {
             *  admin = new AdminEntity
             *  {
             *      Name = LoginInput.Name,
             *      Password = LoginInput.Password.ToMd5()
             *  };
             *  _db.Admins.Add(admin);
             *  await _db.SaveChangesAsync();
             *
             *  await AddSuccess(new OperatorLog
             *  {
             *      Type = OperatorLogType.登录,
             *      Content = $"初始化帐号:{admin.Name}",
             *      Name = LoginInput.Name
             *  });
             * }
             * else
             * {
             *  admin = _db.Admins.FirstOrDefault(x => x.Name == LoginInput.Name);
             *  if (admin == null)
             *  {
             *      ErrorMessage = "账号或密码错误";
             *      HasSetAdmin = _db.Admins.Count() != 0;
             *
             *      await AddError(new OperatorLog
             *      {
             *          Type = OperatorLogType.登录,
             *          Content = JsonConvert.SerializeObject(LoginInput),
             *          Name = LoginInput.Name
             *      });
             *      return Page();
             *  }
             *
             *  if (admin.Password != LoginInput.Password.ToMd5())
             *  {
             *      ErrorMessage = "账号或密码错误";
             *      HasSetAdmin = _db.Admins.Count() != 0;
             *
             *      await AddError(new OperatorLog
             *      {
             *          Type = OperatorLogType.登录,
             *          Content = JsonConvert.SerializeObject(LoginInput),
             *          Name = LoginInput.Name
             *      });
             *      return Page();
             *  }
             * }
             *
             * var claims = new[] {
             *  new Claim(ClaimTypes.NameIdentifier, admin.Id.ToString()),
             *  new Claim(ClaimTypes.Name, admin.Name)
             * };
             *
             *
             *
             * var claimsIdentity = new ClaimsIdentity(claims, "Default");
             * ClaimsPrincipal user = new ClaimsPrincipal(claimsIdentity);
             *
             * await HttpContext.SignInAsync("admin", user, new AuthenticationProperties { IsPersistent = true, AllowRefresh = true, ExpiresUtc = DateTimeOffset.Now.AddDays(30) });
             *
             * await AddSuccess(new OperatorLog
             * {
             *  Type = OperatorLogType.登录,
             *  Content = "",
             *  Name = LoginInput.Name
             * });
             *
             *
             * return RedirectToPage("Index");
             */
        }
Esempio n. 4
0
        public void Login_Return_Login_Success_If_Right()
        {
            Admin admin = InitFakeEntity.GetFakeAdmin();

            UsingDbContext(ctx => ctx.Admin.Add(admin));
            var result = _iAdminAppSerice.Login(admin.UserName, admin.Password);

            result.ErrorCount.ShouldBe(0);
            result.LockMintues.ShouldBe(0);
            result.LoginResult.ShouldBe(LoginResult.Success);
        }