Exemplo n.º 1
0
        public IActionResult Register([FromBody] LoginPasswordRequest request)
        {
            if (db.Users.FirstOrDefault(user => user.Username == request.username) != null)
            {
                return(BadRequest(new { errorText = "Пользователь уже существует" }));
            }
            if (db.Users.FirstOrDefault(user => user.Email == request.email) != null)
            {
                return(BadRequest(new { errorText = "Пользователь с такой почтой уже существует" }));
            }

            var user = new User
            {
                Username = request.username,
                Email    = request.email,
            };

            db.Users.Add(user);
            db.SaveChanges();

            var currentUserId = db.Users.FirstOrDefault(u => u.Username == request.username).ID;

            var password = new Password
            {
                UserId = currentUserId,
                Pwd    = request.password,
            };

            db.Passwords.Add(password);
            db.SaveChanges();

            return(Login(request));
        }
Exemplo n.º 2
0
        public IActionResult Login(UserViewModel model, string backto)
        {
            if (bool.TryParse(settings["NoLogin"], out bool noLogin) && noLogin)
            {
                return(NotFound());
            }


            if (!ModelState.IsValid)
            {
                return(View("Bind", model));
            }
            var uniFLOWRestServiceURL = this.GetUniFLOWRestServiceURL();
            var req = new LoginPasswordRequest()
            {
                UniFLOWRestServiceURL = uniFLOWRestServiceURL, Login = model.UserName.Trim(), Password = model.Password.Trim()
            };
            var checkResult = _uniflow.CheckUser(req);

            _logger.LogInformation(string.Format("[HomeController] [Login] CheckUser result:{0}", checkResult.Result.Value.Code));
            if (checkResult.Result.Value.Code != "0")
            {
                ModelState.AddModelError("errorMsg", "用户名或密码错误!");
                return(View("Bind", model));
            }

            var bindId   = checkResult.Result.Value.BindId;
            var externId = model.UserName.Trim().ToLower();
            var type     = "LDAPLogin";

            HttpContext.Session.SetExternId(externId, type);

            //var bindResult = _uniflow.Bind(
            //    new BindExternalIdRequest
            //    {
            //        UniFLOWRestServiceURL = HttpContext.Session.GetUniFLOWRestServiceURL(),
            //        ExternalId = externId,
            //        Type = type,
            //        BindId = bindId,
            //    });
            _logger.LogInformation("[HomeController] [Login] [Bind] BindResult:" + bindResult.Result.Value.Code);
            if (bindResult.Result.Value.Code != "0")
            {
                ModelState.AddModelError("errorMsg", bindResult.Result.Value.Message);
                return(View("Bind", model));
            }

            HttpContext.Session.SetBindId(bindId);
            HttpContext.Session.SetLdapLoginId(model.UserName);

            if (!string.IsNullOrEmpty(backto))
            {
                return(Redirect(WebUtility.UrlDecode(backto)));
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 3
0
        public IActionResult Login([FromBody] LoginPasswordRequest request)
        {
            var identity = GetIdentity(request.username, request.password);

            if (identity == null)
            {
                return(BadRequest(new { errorText = "Неверное имя пользователя или пароль." }));
            }

            string encodedJwt = TokenGenerator.GetJWTToken(identity);

            User user = db.Users.FirstOrDefault(user => user.Username == request.username);

            var result = new
            {
                id       = user.ID,
                username = user.Username,
                role     = user.Role,
                token    = encodedJwt,
            };

            return(Json(result));
        }