Example #1
0
        public async Task <ActionResult <StatusResponse> > Login(string code)
        {
            var url = string.Format(
                settings["WxApp:UrlPattern"],
                settings["WxApp:AppId"],
                settings["WxApp:Secret"],
                code
                );

            var res = await RequestUtil.HttpGetAsync(url.ToString());

            var response = JsonConvert.DeserializeAnonymousType(res, new
            {
                errcode = 0,
                errmsg  = "",
                openid  = "",
            });

            //dynamic response = JsonConvert.DeserializeObject(res);
            if (response.errcode != 0)
            {
                return(new StatusResponse
                {
                    Code = Error.Codes.ExternalError.AsString(),
                    Message = Error.Codes.ExternalError.AsMessage(
                        response.errcode, response.errmsg),
                });
            }

            var openId = response.openid as string;

            HttpContext.Session.SetExternId(openId, "WeChatAppOpenID");
            _logger.LogInformation(string.Format("[WeChatAppController] [Login] OpenId:{0}", openId));
            var checkResult = _uniflow.CheckBind(
                new ExternalIdRequest {
                ExternalId = openId, Type = "WeChatAppOpenID"
            });

            _logger.LogInformation(string.Format("[WeChatAppController] [Login] [CheckBind] Code:{0}", checkResult.Value.Code));
            if (checkResult.Value.Code == "0")
            {
                var bindId = checkResult.Value.BindId;
                HttpContext.Session.SetBindId(bindId);
                HttpContext.Session.SetLdapLoginId(checkResult.Value.LdapLoginId);
            }

            return(new StatusResponse
            {
                Code = checkResult.Value.Code,
                Message = checkResult.Value.Message,
            });
        }
Example #2
0
        public IActionResult OAuth2Callback(string code, string backto, string state)
        {
            var ua = Request.Headers["User-Agent"].ToString();

            string externId = "", type = "";

            if (Regex.IsMatch(ua, "MicroMessenger", RegexOptions.IgnoreCase)) // wechat
            {
                var isWxWork = Regex.IsMatch(ua, "wxwork", RegexOptions.IgnoreCase);
                if (isWxWork)
                {
                    (externId, type) = WxWorkCallback(code);
                }
                else
                {
                    (externId, type) = WxCallback(code);
                }
            }
            else
            {
                _logger.LogInformation("Not supported oauth provider.");
                return(View("Error", new ErrorViewModel {
                    Message = "Not supported oauth provider."
                }));
            }

            HttpContext.Session.SetExternId(externId, type);

            _logger.LogInformation($"OAuth2 result: {externId} ({type})");
            var checkResult = _uniflow.CheckBind(
                new ExternalIdRequest {
                ExternalId = externId, Type = type
            });

            _logger.LogInformation("CheckBind: " + JsonConvert.SerializeObject(checkResult.Value));

            if (checkResult.Value.Code != "0")
            {
                return(RedirectToAction("Bind", new { backto }));
            }

            var bindId = checkResult.Value.BindId;

            HttpContext.Session.SetBindId(bindId);
            HttpContext.Session.SetLdapLoginId(checkResult.Value.LdapLoginId);

            if (!string.IsNullOrEmpty(backto))
            {
                return(Redirect(WebUtility.UrlDecode(backto)));
            }
            return(RedirectToAction("Index"));
        }