Esempio n. 1
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 req = new LoginPasswordRequest()
            {
                Login = model.UserName.Trim(), Password = model.Password.Trim()
            };
            var checkResult = _uniflow.CheckUser(req);

            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
            {
                ExternalId = externId,
                Type       = type,
                BindId     = bindId,
            });

            _logger.LogInformation("[HomeController] [Login] [Bind] Code:" + 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"));
        }
Esempio n. 2
0
        public async Task <ActionResult <StatusResponse> > Bind(LoginPasswordRequest req)
        {
            var(openId, type) = HttpContext.Session.GetExternId();
            _logger.LogInformation(string.Format("[WeChatAppController] [Bind] OpenId:{0},Type:{1}", openId, type));
            if (string.IsNullOrEmpty(openId))
            {
                return(new StatusResponse
                {
                    Code = Error.Codes.LoginRequired.AsString(),
                    Message = Error.Codes.LoginRequired.AsMessage(),
                });
            }

            var checkResult = await _uniflow.CheckUser(req);

            _logger.LogInformation(string.Format("[WeChatAppController] [Bind] [CheckUser] Code:{0}", checkResult.Value.Code));
            if (checkResult.Value.Code != "0")
            {
                return(new StatusResponse
                {
                    Code = checkResult.Value.Code,
                    Message = checkResult.Value.Message,
                });
            }

            var bindId = checkResult.Value.BindId;

            HttpContext.Session.SetBindId(bindId);

            var bindResult = await _uniflow.Bind(
                new BindExternalIdRequest
            {
                ExternalId = openId,
                Type       = "WeChatAppOpenID",
                BindId     = bindId,
            });

            return(new StatusResponse
            {
                Code = bindResult.Value.Code,
                Message = bindResult.Value.Message,
            });
        }
Esempio n. 3
0
        public async Task <ActionResult <BindStatusResponse> > CheckUser(LoginPasswordRequest req)
        {
            if (!ModelState.IsValid)
            {
                var error = ModelState.First(m => m.Value.Errors.Count > 0);
                return(new BindStatusResponse
                {
                    Code = Error.Codes.InvalidData.AsString(),
                    Message = Error.Codes.InvalidData.AsMessage(
                        error.Key, error.Value.Errors[0].ErrorMessage),
                });
            }

            try
            {
                string baseurl = settings["UniflowService:Url"];
                string key     = settings["UniflowService:EncryptKey"];
                string salt    = EncryptUtil.CreateCryptographicallySecureGuid();

                string login    = EncryptUtil.Encrypt(req.Login.Trim(), key, salt);
                string password = EncryptUtil.Encrypt(req.Password.Trim(), key, salt);

                string url = $"{baseurl}/WECHAT/CHECKUSER/{login}/{password}";
                _logger.LogTrace("Get " + url);
                var result = await RequestUtil.HttpGetAsync(url);

                _logger.LogTrace("Response: " + result);

                var xdoc    = XElement.Parse(result);
                var ns      = xdoc.GetDefaultNamespace();
                var status  = xdoc.Element(ns.GetName("Status")).Value;
                var bindId  = xdoc.Element(ns.GetName("UserRef")).Value;
                var code    = xdoc.Element(ns.GetName("ErrorCode")).Value;
                var message = status == "0" ? Error.Codes.OK.AsMessage() :
                              code.AsCode().AsMessage(xdoc.Element(ns.GetName("ErrorDesc")).Value);

                var response = new BindStatusResponse
                {
                    Code        = code,
                    Message     = message,
                    BindId      = bindId,
                    LdapLoginId = req.Login,
                };

                if (status != "0")
                {
                    return(response);
                }

                var bind = _ctx.BindUsers.Find(bindId);
                if (bind == null)
                {
                    _ctx.BindUsers.Add(new BindUser
                    {
                        BindUserId = bindId,
                        UserLogin  = req.Login,
                    });
                    await _ctx.SaveChangesAsync();
                }
                return(response);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "error");
                return(new BindStatusResponse
                {
                    Code = Error.Codes.Exception.AsString(),
                    Message = Error.Codes.Exception.AsMessage(
                        ex.Message),
                });
            }
        }