public async Task <IActionResult> OAuth([FromBody] GithubOAuth payload) { string client_id = _env.GetStringValue("client_id"); string client_secret = _env.GetStringValue("client_secret"); // Initial request for token HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, $"https://github.com/login/oauth/access_token?client_id={client_id}&client_secret={client_secret}&code={payload.Code}"); request.Headers.Add("Accept", "application/json"); HttpClient client = _clientFactory.CreateClient(); HttpResponseMessage response = await client.SendAsync(request); if (response.IsSuccessStatusCode) { // Extract token from response body JsonDocument document = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync()); string token = document.RootElement.GetProperty("access_token").GetString(); // Request user data using the token request = new HttpRequestMessage(HttpMethod.Get, "https://api.github.com/user"); request.Headers.Add("Authorization", $"token {token}"); request.Headers.Add("User-Agent", "IssueNest"); response = await client.SendAsync(request); // Read response Console.WriteLine(await response.Content.ReadAsStringAsync()); return(Ok()); } return(Unauthorized()); }
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)); }
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)); } }
public AuthorizationController(GithubOAuth githubOAuth) { this._githubOAuth = githubOAuth; }