Exemplo n.º 1
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 = req.UniFLOWRestServiceURL;
                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, "CheckUser Error");
                return(new BindStatusResponse
                {
                    Code = Error.Codes.Exception.AsString(),
                    Message = Error.Codes.Exception.AsMessage(
                        ex.Message),
                });
            }
        }