feat:统一登录模块对接待完善

develop
old易 2023-11-08 20:45:28 +08:00
parent ef7ef0d056
commit ab7712e22f
8 changed files with 249 additions and 2 deletions

View File

@ -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()
{
}
/// <summary>
///Oauth2验证回调
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
[HttpGet]
[Route("callback")]
public async Task<MessageModel<string>> Callback(string code, string state)
{
string jwtStr = string.Empty;
bool suc = false;
var gettoken = CasdoorHttpHelper.Post_AccessToken<CasdoorGetTokenResponse>(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<string>()
{
success = suc,
msg = suc ? "success" : "fail",
response = jwtStr
};
}
}
}

View File

@ -1144,6 +1144,13 @@
</summary>
<returns></returns>
</member>
<member name="M:New_College.Controllers.OauthController.Callback(System.String,System.String)">
<summary>
Oauth2验证回调
</summary>
<param name="code"></param>
<returns></returns>
</member>
<member name="T:New_College.Controllers.PermissionController">
<summary>
菜单管理

View File

@ -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();

View File

@ -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
}
}

View File

@ -16,4 +16,24 @@ namespace New_College
public static string endpoint { get; set; }
}
public static class CasdoorConfig
{
/// <summary>
///验证接口入口
/// </summary>
public static string Endpoint { get; set; }
public static string ClientId { get; set; }
public static string ClientSecret { get; set; }
/// <summary>
/// 回调登录地址
/// </summary>
public static string CallbackPath { get; set; }
}
}

View File

@ -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
{
/// <summary>
/// 获取accesstoken
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="code"></param>
/// <returns></returns>
public static T Post_AccessToken<T>(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<T>(responseStr);
return obj;
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 刷新token
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="refresh_token"></param>
/// <param name="scope"></param>
/// <returns></returns>
public static T Post_RefreshToken<T>(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<T>(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; }
}
}
}

View File

@ -16,6 +16,7 @@
<ItemGroup>
<PackageReference Include="sqlSugarCore" Version="5.0.0.15" />
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>
<ItemGroup>

View File

@ -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; }
}
}