diff --git a/New_College.Api/Controllers/OAuthController.cs b/New_College.Api/Controllers/OAuthController.cs new file mode 100644 index 0000000..58f974f --- /dev/null +++ b/New_College.Api/Controllers/OAuthController.cs @@ -0,0 +1,69 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using New_College.AuthHelper.OverWrite; +using New_College.Common; +using New_College.Common.Helper; +using New_College.Model; +using New_College.Model.ViewModels; +using NPOI.SS.Formula.Functions; +using System.Threading.Tasks; + +namespace New_College.Controllers +{ + + [AllowAnonymous] + [Route("api/oauth")] + public class OauthController : Controller + { + + public OauthController() + { + + } + + + + + + /// + ///Oauth2验证回调 + /// + /// + /// + [HttpGet] + [Route("callback")] + public async Task> Callback(string code, string state) + { + string jwtStr = string.Empty; + bool suc = false; + + var gettoken = CasdoorHttpHelper.Post_AccessToken(code); + //var user = await _sysUserInfoServices.GetUserRoleNameStr(name, MD5Helper.MD5Encrypt32(pass)); + //if (user != null) + //{ + + TokenModelJwt tokenModel = new TokenModelJwt { Uid = 1, Role = gettoken.access_token }; + + jwtStr = JwtHelper.IssueJwt(tokenModel); + suc = true; + //} + //else + //{ + // jwtStr = "login fail!!!"; + //} + + + return new MessageModel() + { + success = suc, + msg = suc ? "success" : "fail", + response = jwtStr + }; + } + + + + + + } +} diff --git a/New_College.Api/New_College.xml b/New_College.Api/New_College.xml index 237b14d..b1269cf 100644 --- a/New_College.Api/New_College.xml +++ b/New_College.Api/New_College.xml @@ -1144,6 +1144,13 @@ + + + Oauth2验证回调 + + + + 菜单管理 diff --git a/New_College.Api/Startup.cs b/New_College.Api/Startup.cs index ea641a1..56913b2 100644 --- a/New_College.Api/Startup.cs +++ b/New_College.Api/Startup.cs @@ -21,6 +21,9 @@ using System.Reflection; using New_College.Model.ViewModels; using Essensoft.AspNetCore.Payment.WeChatPay; using Essensoft.AspNetCore.Payment.Alipay; +using Microsoft.AspNetCore.Http; +using IdentityModel; +using Microsoft.AspNetCore.Authentication.Cookies; namespace New_College { @@ -64,7 +67,14 @@ namespace New_College AliYunOssConfig.bucket = Appsettings.app(new string[] { "AliYunOss", "bucket" }).ObjToString(); AliYunOssConfig.endpoint = Appsettings.app(new string[] { "AliYunOss", "endpoint" }).ObjToString(); - + + + ///三方验证接口 + CasdoorConfig.Endpoint = Appsettings.app(new string[] { "Casdoor", "Endpoint" }); + CasdoorConfig.ClientId = Appsettings.app(new string[] { "Casdoor", "ClientId" }); + CasdoorConfig.ClientSecret = Appsettings.app(new string[] { "Casdoor", "ClientSecret" }); + CasdoorConfig.CallbackPath = Appsettings.app(new string[] { "Casdoor", "CallbackPath" }); + services.AddMemoryCacheSetup(); diff --git a/New_College.Api/appsettings.json b/New_College.Api/appsettings.json index 344d533..f763510 100644 --- a/New_College.Api/appsettings.json +++ b/New_College.Api/appsettings.json @@ -206,5 +206,15 @@ "accessKeySecret": "EvC8MjRaQC1kHubgU4MtecZnofOb0v", "bucket": "static-data-ycymedu", "endpoint": "https://oss-cn-shanghai.aliyuncs.com" - } + }, + "Casdoor": { + "Endpoint": "http://192.168.103.119:8000", + "OrganizationName": "六纬生涯", + "ApplicationName": "六纬生涯", + "ApplicationType": "webapi", + "ClientId": "ae6bdccc3a7821232b31", + "ClientSecret": "ac4f5eeb78528f2b89d56d2c3148d717c5db5e99", + "CallbackPath": "http://192.168.103.119:8000/login/oauth/authorize?client_id=ae6bdccc3a7821232b31&response_type=code&redirect_uri=http://192.168.103.100:8083/callback&scope=read&state=casdoor", + "RequireHttpsMetadata": false + } } diff --git a/New_College.Common/GlobalVar/AliYunOssConfig.cs b/New_College.Common/GlobalVar/AliYunOssConfig.cs index 8f37601..9325107 100644 --- a/New_College.Common/GlobalVar/AliYunOssConfig.cs +++ b/New_College.Common/GlobalVar/AliYunOssConfig.cs @@ -16,4 +16,24 @@ namespace New_College public static string endpoint { get; set; } } + + + + public static class CasdoorConfig + { + + /// + ///验证接口入口 + /// + public static string Endpoint { get; set; } + public static string ClientId { get; set; } + public static string ClientSecret { get; set; } + + /// + /// 回调登录地址 + /// + public static string CallbackPath { get; set; } + } + + } diff --git a/New_College.Common/Helper/CasdoorHttpHelper.cs b/New_College.Common/Helper/CasdoorHttpHelper.cs new file mode 100644 index 0000000..e0ad695 --- /dev/null +++ b/New_College.Common/Helper/CasdoorHttpHelper.cs @@ -0,0 +1,111 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; + +namespace New_College.Common +{ + public class CasdoorHttpHelper + { + + /// + /// 获取accesstoken + /// + /// + /// + /// + public static T Post_AccessToken(string code) where T : new() + { + var authinfo = new T(); + try + { + string requestUri = string.Format("{0}/api/login/oauth/access_token", CasdoorConfig.Endpoint); + var httpClientHandler = new HttpClientHandler + { + ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true + }; + using (HttpClient httpClient = new HttpClient(httpClientHandler)) + { + var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(new CasdoorRequest() { code = code, grant_type = "authorization_code", client_id = CasdoorConfig.ClientId, client_secret = CasdoorConfig.ClientSecret }), Encoding.UTF8, "application/json"); + var responseStr = httpClient.PostAsync(requestUri, content).Result.Content.ReadAsStringAsync().Result; + var obj = JsonConvert.DeserializeObject(responseStr); + return obj; + } + } + catch (Exception ex) + { + throw ex; + } + } + + + + /// + /// 刷新token + /// + /// + /// + /// + /// + public static T Post_RefreshToken(string refresh_token, string scope) where T : new() + { + var authinfo = new T(); + try + { + string requestUri = string.Format("{0}/api/login/oauth/refresh_token", CasdoorConfig.Endpoint); + var httpClientHandler = new HttpClientHandler + { + ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true + }; + using (HttpClient httpClient = new HttpClient(httpClientHandler)) + { + var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(new CasdoorRefeshToken() { refresh_token = refresh_token, scope = "built-in", grant_type = "authorization_code", client_id = CasdoorConfig.ClientId, client_secret = CasdoorConfig.ClientSecret }), Encoding.UTF8, "application/json"); + var responseStr = httpClient.PostAsync(requestUri, content).Result.Content.ReadAsStringAsync().Result; + var obj = JsonConvert.DeserializeObject(responseStr); + return obj; + } + } + catch (Exception ex) + { + throw ex; + } + } + + + + + private class CasdoorRequest + { + public string grant_type { get; set; } + public string client_id { get; set; } + public string client_secret { get; set; } + public string code { get; set; } + } + + + + private class CasdoorRefeshToken + { + + public string grant_type { get; set; } + public string client_id { get; set; } + public string client_secret { get; set; } + public string scope { get; set; } + + public string refresh_token { get; set; } + + } + + } + + + + + + + + + + +} diff --git a/New_College.Model/New_College.Model.csproj b/New_College.Model/New_College.Model.csproj index bf70a47..f429710 100644 --- a/New_College.Model/New_College.Model.csproj +++ b/New_College.Model/New_College.Model.csproj @@ -16,6 +16,7 @@ + diff --git a/New_College.Model/ViewModels/Result/CasDoorTokenResult.cs b/New_College.Model/ViewModels/Result/CasDoorTokenResult.cs new file mode 100644 index 0000000..28abe1c --- /dev/null +++ b/New_College.Model/ViewModels/Result/CasDoorTokenResult.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace New_College.Model.ViewModels +{ + public class CasdoorGetTokenResponse + { + public string access_token { get; set; } + public int expires_in { get; set; } + public string id_token { get; set; } + public string refresh_token { get; set; } + public string scope { get; set; } + public string token_type { get; set; } + public string error { get; set; } + public string error_description { get; set; } + } + +}