Esempio n. 1
0
        public IActionResult Index(
            string type,
            [FromServices] BaiduOAuth baiduOAuth,
            [FromServices] WechatOAuth wechatOAuth,
            [FromServices] GitlabOAuth gitlabOAuth,
            [FromServices] GiteeOAuth giteeOAuth,
            [FromServices] GithubOAuth githubOAuth,
            [FromServices] HuaweiOAuth huaweiOAuth,
            [FromServices] CodingOAuth codingOAuth,
            [FromServices] SinaWeiboOAuth sinaWeiboOAuth,
            [FromServices] AlipayOAuth alipayOAuth,
            [FromServices] QQOAuth qqOAuth,
            [FromServices] OSChinaOAuth oschinaOAuth,
            [FromServices] DouYinOAuth douYinOAuth
            )
        {
            var redirectUrl = "";

            switch (type.ToLower())
            {
            case "baidu":
            {
                redirectUrl = baiduOAuth.GetAuthorizeUrl();
                break;
            }

            case "wechat":
            {
                redirectUrl = wechatOAuth.GetAuthorizeUrl();
                break;
            }

            case "gitlab":
            {
                redirectUrl = gitlabOAuth.GetAuthorizeUrl();
                break;
            }

            case "gitee":
            {
                redirectUrl = giteeOAuth.GetAuthorizeUrl();
                break;
            }

            case "github":
            {
                redirectUrl = githubOAuth.GetAuthorizeUrl();
                break;
            }

            case "huawei":
            {
                redirectUrl = huaweiOAuth.GetAuthorizeUrl();
                break;
            }

            case "coding":
            {
                redirectUrl = codingOAuth.GetAuthorizeUrl();
                break;
            }

            case "sinaweibo":
            {
                redirectUrl = sinaWeiboOAuth.GetAuthorizeUrl();
                break;
            }

            case "alipay":
            {
                redirectUrl = alipayOAuth.GetAuthorizeUrl();
                break;
            }

            case "qq":
            {
                redirectUrl = qqOAuth.GetAuthorizeUrl();
                break;
            }

            case "oschina":
            {
                redirectUrl = oschinaOAuth.GetAuthorizeUrl();
                break;
            }

            case "douyin":
            {
                redirectUrl = douYinOAuth.GetAuthorizeUrl();
                break;
            }

            default:
                return(ReturnToError($"没有实现【{type}】登录方式!"));
            }
            return(Redirect(redirectUrl));
        }
Esempio n. 2
0
        public async Task <IActionResult> LoginCallback(
            string type,
            [FromServices] BaiduOAuth baiduOAuth,
            [FromServices] WechatOAuth wechatOAuth,
            [FromServices] GitlabOAuth gitlabOAuth,
            [FromServices] GiteeOAuth giteeOAuth,
            [FromServices] GithubOAuth githubOAuth,
            [FromServices] HuaweiOAuth huaweiOAuth,
            [FromServices] CodingOAuth codingOAuth,
            [FromServices] SinaWeiboOAuth sinaWeiboOAuth,
            [FromServices] AlipayOAuth alipayOAuth,
            [FromServices] QQOAuth qqOAuth,
            [FromServices] OSChinaOAuth oschinaOAuth,
            [FromServices] DouYinOAuth douYinOAuth,
            [FromQuery] string code,
            [FromQuery] string state,
            [FromQuery] string error_description = "")
        {
            Console.WriteLine($"LoginCallback [{HttpContext.Request.Path}]");
            try
            {
                if (!string.IsNullOrEmpty(error_description))
                {
                    throw new Exception(error_description);
                }
                HttpContext.Session.SetString("OAuthPlatform", type.ToLower());
                switch (type.ToLower())
                {
                case "baidu":
                {
                    var authorizeResult = await baiduOAuth.AuthorizeCallback(code, state);

                    if (!authorizeResult.IsSccess)
                    {
                        throw new Exception(authorizeResult.ErrorMessage);
                    }
                    HttpContext.Session.Set("OAuthUser", authorizeResult.UserInfo.ToUserInfoBase());
                    HttpContext.Session.Set("OAuthUserDetail", authorizeResult.UserInfo, true);
                    break;
                }

                case "wechat":
                {
                    var authorizeResult = await wechatOAuth.AuthorizeCallback(code, state);

                    if (!authorizeResult.IsSccess)
                    {
                        throw new Exception(authorizeResult.ErrorMessage);
                    }
                    HttpContext.Session.Set("OAuthUser", authorizeResult.UserInfo.ToUserInfoBase());
                    HttpContext.Session.Set("OAuthUserDetail", authorizeResult.UserInfo, true);
                    break;
                }

                case "gitlab":
                {
                    var authorizeResult = await gitlabOAuth.AuthorizeCallback(code, state);

                    if (!authorizeResult.IsSccess)
                    {
                        throw new Exception(authorizeResult.ErrorMessage);
                    }
                    HttpContext.Session.Set("OAuthUser", authorizeResult.UserInfo.ToUserInfoBase());
                    HttpContext.Session.Set("OAuthUserDetail", authorizeResult.UserInfo, true);
                    break;
                }

                case "gitee":
                {
                    var authorizeResult = await giteeOAuth.AuthorizeCallback(code, state);

                    if (!authorizeResult.IsSccess)
                    {
                        throw new Exception(authorizeResult.ErrorMessage);
                    }
                    HttpContext.Session.Set("OAuthUser", authorizeResult.UserInfo.ToUserInfoBase());
                    HttpContext.Session.Set("OAuthUserDetail", authorizeResult.UserInfo, true);
                    break;
                }

                case "github":
                {
                    var authorizeResult = await githubOAuth.AuthorizeCallback(code, state);

                    if (!authorizeResult.IsSccess)
                    {
                        throw new Exception(authorizeResult.ErrorMessage);
                    }
                    HttpContext.Session.Set("OAuthUser", authorizeResult.UserInfo.ToUserInfoBase());
                    HttpContext.Session.Set("OAuthUserDetail", authorizeResult.UserInfo, true);
                    break;
                }

                case "huawei":
                {
                    var authorizeResult = await huaweiOAuth.AuthorizeCallback(code, state);

                    if (!authorizeResult.IsSccess)
                    {
                        throw new Exception(authorizeResult.ErrorMessage);
                    }
                    HttpContext.Session.Set("OAuthUser", authorizeResult.UserInfo.ToUserInfoBase());
                    HttpContext.Session.Set("OAuthUserDetail", authorizeResult.UserInfo, true);
                    break;
                }

                case "coding":
                {
                    var authorizeResult = await codingOAuth.AuthorizeCallback(code, state);

                    if (!authorizeResult.IsSccess)
                    {
                        throw new Exception(authorizeResult.ErrorMessage);
                    }
                    HttpContext.Session.Set("OAuthUser", authorizeResult.UserInfo.ToUserInfoBase());
                    HttpContext.Session.Set("OAuthUserDetail", authorizeResult.UserInfo, true);
                    break;
                }

                case "sinaweibo":
                {
                    var authorizeResult = await sinaWeiboOAuth.AuthorizeCallback(code, state);

                    if (!authorizeResult.IsSccess)
                    {
                        throw new Exception(authorizeResult.ErrorMessage);
                    }
                    HttpContext.Session.Set("OAuthUser", authorizeResult.UserInfo.ToUserInfoBase());
                    HttpContext.Session.Set("OAuthUserDetail", authorizeResult.UserInfo, true);
                    break;
                }

                case "alipay":
                {
                    code = HttpContext.Request.Query["auth_code"];
                    var authorizeResult = await alipayOAuth.AuthorizeCallback(code, state);

                    if (!authorizeResult.IsSccess)
                    {
                        throw new Exception(authorizeResult.ErrorMessage);
                    }
                    HttpContext.Session.Set("OAuthUser", authorizeResult.UserInfo.ToUserInfoBase());
                    HttpContext.Session.Set("OAuthUserDetail", authorizeResult.UserInfo, true);
                    break;
                }

                case "qq":
                {
                    var authorizeResult = await qqOAuth.AuthorizeCallback(code, state);

                    if (!authorizeResult.IsSccess)
                    {
                        throw new Exception(authorizeResult.ErrorMessage);
                    }
                    HttpContext.Session.Set("OAuthUser", authorizeResult.UserInfo.ToUserInfoBase());
                    HttpContext.Session.Set("OAuthUserDetail", authorizeResult.UserInfo, true);
                    break;
                }

                case "oschina":
                {
                    var authorizeResult = await oschinaOAuth.AuthorizeCallback(code, state);

                    if (!authorizeResult.IsSccess)
                    {
                        throw new Exception(authorizeResult.ErrorMessage);
                    }
                    HttpContext.Session.Set("OAuthUser", authorizeResult.UserInfo.ToUserInfoBase());
                    HttpContext.Session.Set("OAuthUserDetail", authorizeResult.UserInfo, true);
                    break;
                }

                case "douyin":
                {
                    var authorizeResult = await douYinOAuth.AuthorizeCallback(code, state);

                    if (!authorizeResult.IsSccess)
                    {
                        throw new Exception(authorizeResult.ErrorMessage);
                    }
                    HttpContext.Session.Set("OAuthUser", authorizeResult.UserInfo.ToUserInfoBase());
                    HttpContext.Session.Set("OAuthUserDetail", authorizeResult.UserInfo, true);
                    break;
                }

                default:
                    throw new Exception($"没有实现【{type}】登录回调!");
                }
                return(RedirectToAction("Result"));
            }
            catch (Exception ex)
            {
                HttpContext.Session.Remove("OAuthPlatform");
                HttpContext.Session.Remove("OAuthUser");
                HttpContext.Session.Remove("OAuthUserDetail");
                Console.WriteLine(ex.ToString());
                return(ReturnToError(ex.Message));
            }
        }