|
@@ -31,6 +31,7 @@ using static TEAMModelOS.SDK.SchoolService;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using TEAMModelOS.Filter;
|
|
using TEAMModelOS.Filter;
|
|
using TEAMModelOS.Controllers.Third.Xkw;
|
|
using TEAMModelOS.Controllers.Third.Xkw;
|
|
|
|
+using Microsoft.Extensions.Primitives;
|
|
|
|
|
|
namespace TEAMModelOS.Controllers
|
|
namespace TEAMModelOS.Controllers
|
|
{
|
|
{
|
|
@@ -103,19 +104,33 @@ namespace TEAMModelOS.Controllers
|
|
public async Task<IActionResult> Aauth() {
|
|
public async Task<IActionResult> Aauth() {
|
|
//https://ssoserviceurl/oauth2/authorize?client_id=APPKEY&openid=OPENID=&service=SERVICE
|
|
//https://ssoserviceurl/oauth2/authorize?client_id=APPKEY&openid=OPENID=&service=SERVICE
|
|
var (tmdid, _, _, school) = HttpContext.GetAuthTokenInfo();
|
|
var (tmdid, _, _, school) = HttpContext.GetAuthTokenInfo();
|
|
- var client = GetOpenAuthClient(tmdid);
|
|
|
|
|
|
+ StringValues accessToken = "";//应该从别的地方获取 不是mvc 无法从Session 获取
|
|
|
|
+ HttpContext.Request.Headers.TryGetValue($"XKW-AccessToken", out accessToken);
|
|
|
|
+ if (!_option.Location.Contains("China"))
|
|
|
|
+ {
|
|
|
|
+ return BadRequest();
|
|
|
|
+ }
|
|
|
|
+ var client = await GetOpenAuthClient(tmdid, accessToken);
|
|
string url = client.GetAuthorizationUrl();
|
|
string url = client.GetAuthorizationUrl();
|
|
return Redirect(url);
|
|
return Redirect(url);
|
|
}
|
|
}
|
|
[HttpGet("authorized")]
|
|
[HttpGet("authorized")]
|
|
- public async Task<IActionResult> Authorized(string code )
|
|
|
|
|
|
+ [AuthToken(Roles = "teacher,admin,area,student")]
|
|
|
|
+ public async Task<IActionResult> Authorized([FromQuery] OAuthCode authCode )
|
|
{
|
|
{
|
|
var (tmdid, _, _, school) = HttpContext.GetAuthTokenInfo();
|
|
var (tmdid, _, _, school) = HttpContext.GetAuthTokenInfo();
|
|
- if (string.IsNullOrEmpty(code))
|
|
|
|
|
|
+ StringValues accessToken = "";//应该从别的地方获取 不是mvc 无法从Session 获取
|
|
|
|
+ HttpContext.Request.Headers.TryGetValue($"XKW-AccessToken", out accessToken);
|
|
|
|
+ if (!_option.Location.Contains("China"))
|
|
|
|
+ {
|
|
|
|
+ return BadRequest();
|
|
|
|
+ }
|
|
|
|
+ //没有获取到codes的情况
|
|
|
|
+ if (string.IsNullOrEmpty(authCode.code))
|
|
{
|
|
{
|
|
return RedirectToAction("Index");
|
|
return RedirectToAction("Index");
|
|
}
|
|
}
|
|
- var client = GetOpenAuthClient(tmdid);
|
|
|
|
|
|
+ var client =await GetOpenAuthClient(tmdid, accessToken);
|
|
string schoolId = null;
|
|
string schoolId = null;
|
|
//UserTxtHelper userHelper = new UserTxtHelper(@USER_PATH);
|
|
//UserTxtHelper userHelper = new UserTxtHelper(@USER_PATH);
|
|
//User currentUser = userHelper.GetUserInfoByUserId(client.UserId);
|
|
//User currentUser = userHelper.GetUserInfoByUserId(client.UserId);
|
|
@@ -123,16 +138,16 @@ namespace TEAMModelOS.Controllers
|
|
//{
|
|
//{
|
|
// schoolId = currentUser.SchoolId;
|
|
// schoolId = currentUser.SchoolId;
|
|
//}
|
|
//}
|
|
- client.GetAccessTokenByCode(code, schoolId);
|
|
|
|
|
|
+ client.GetAccessTokenByCode(authCode.code, schoolId);
|
|
//未登录已认证学科网用户
|
|
//未登录已认证学科网用户
|
|
if (string.IsNullOrEmpty(client.UserId) || "".Equals(client.UserId.Trim()))
|
|
if (string.IsNullOrEmpty(client.UserId) || "".Equals(client.UserId.Trim()))
|
|
{
|
|
{
|
|
- return RedirectToAction("Bind", "Demo", new { openId = client.OpenId, userId = client.UserId });
|
|
|
|
|
|
+ return RedirectToAction("bind", "xkw", new { openId = client.OpenId, userId = client.UserId });
|
|
}
|
|
}
|
|
if (string.IsNullOrEmpty(client.OpenId))
|
|
if (string.IsNullOrEmpty(client.OpenId))
|
|
{
|
|
{
|
|
string errorMsg = client.ErrorMessage;
|
|
string errorMsg = client.ErrorMessage;
|
|
- return RedirectToAction("Bind", "Demo", new { openId = client.OpenId, userId = client.UserId, msg = errorMsg });
|
|
|
|
|
|
+ return RedirectToAction("bind", "xkw", new { openId = client.OpenId, userId = client.UserId, msg = errorMsg });
|
|
}
|
|
}
|
|
|
|
|
|
if (client.IsAuthorized)
|
|
if (client.IsAuthorized)
|
|
@@ -140,8 +155,8 @@ namespace TEAMModelOS.Controllers
|
|
//用session记录access token
|
|
//用session记录access token
|
|
//Session["access_token"] = client.AccessToken;
|
|
//Session["access_token"] = client.AccessToken;
|
|
//用cookie记录userId
|
|
//用cookie记录userId
|
|
- /// Response.AppendCookie(new HttpCookie("userId", client.UserId) { Expires = DateTime.Now.AddDays(7) });
|
|
|
|
- return RedirectToAction("Bind", "Demo", new { client.AccessToken,openId = client.OpenId, userId = client.UserId });
|
|
|
|
|
|
+ /// Response.AppendCookie(new HttpCookie("userId", client.UserId) { Expires = DateTime.Now.AddDays(7) });
|
|
|
|
+ return RedirectToAction("bind", "xkw", new { client.AccessToken,openId = client.OpenId, userId = client.UserId });
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
@@ -192,25 +207,37 @@ namespace TEAMModelOS.Controllers
|
|
/// 封装一个方法来初始化OpenAuth客户端
|
|
/// 封装一个方法来初始化OpenAuth客户端
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
- private XkwOAuthClient GetOpenAuthClient(string tmdid)
|
|
|
|
|
|
+ private async Task<XkwOAuthClient> GetOpenAuthClient(string tmdid,string accessToken)
|
|
{
|
|
{
|
|
- // var accessToken = Session["access_token"] == null ? string.Empty : (string)Session["access_token"];
|
|
|
|
-
|
|
|
|
- string accessToken = "";//应该从别的地方获取 不是mvc 无法从Session 获取
|
|
|
|
|
|
+ //var accessToken = Session["access_token"] == null ? string.Empty : (string)Session["access_token"];
|
|
//var userId = Request.Cookies["userId"] == null ? string.Empty : Request.Cookies["userId"].Value;
|
|
//var userId = Request.Cookies["userId"] == null ? string.Empty : Request.Cookies["userId"].Value;
|
|
var userId = tmdid;//直接传递获取
|
|
var userId = tmdid;//直接传递获取
|
|
//var openId = xkwOAuthTxtHelper.GetOpenIdByUserId(userId);
|
|
//var openId = xkwOAuthTxtHelper.GetOpenIdByUserId(userId);
|
|
var openId = "";//直接从数据库获取
|
|
var openId = "";//直接从数据库获取
|
|
- // var settings = ConfigurationManager.AppSettings;
|
|
|
|
- // var client = new XkwOAuthClient(settings["OAuth_Xkw_AppKey"], settings["OAuth_Xkw_AppSecret"], settings["OAuth_Xkw_RedirectUrl"], settings["OAuth_Xkw_OAuthHost"], accessToken, openId, userId);
|
|
|
|
- string OAuth_Xkw_AppKey = "";//直接从配置文件获取
|
|
|
|
- string OAuth_Xkw_AppSecret = "";//直接从配置文件获取
|
|
|
|
- string OAuth_Xkw_RedirectUrl = "";//直接从配置文件获取
|
|
|
|
- string OAuth_Xkw_OAuthHost = "";//直接从配置文件获取
|
|
|
|
- string OAuth_Xkw_ServiceUrl = "";
|
|
|
|
- var client = new XkwOAuthClient(OAuth_Xkw_AppKey, OAuth_Xkw_AppSecret, OAuth_Xkw_RedirectUrl, OAuth_Xkw_OAuthHost, accessToken, openId, userId);
|
|
|
|
- client.SERVICE_URL = OAuth_Xkw_ServiceUrl;
|
|
|
|
- return client;
|
|
|
|
|
|
+ // var settings = ConfigurationManager.AppSettings;
|
|
|
|
+ // var client = new XkwOAuthClient(settings["OAuth_Xkw_AppKey"], settings["OAuth_Xkw_AppSecret"], settings["OAuth_Xkw_RedirectUrl"], settings["OAuth_Xkw_OAuthHost"], accessToken, openId, userId);
|
|
|
|
+
|
|
|
|
+ var table = _azureStorage.GetCloudTableClient().GetTableReference("IESOAuth");
|
|
|
|
+ string RowKey = "Xkw";
|
|
|
|
+ if (_option.Location.Contains("Test", StringComparison.OrdinalIgnoreCase) || _option.Location.Contains("Dep", StringComparison.OrdinalIgnoreCase)) {
|
|
|
|
+ RowKey = "Xkw-Test";
|
|
|
|
+ }
|
|
|
|
+ List<OAuthComConfig> configs = await table.FindListByDict<OAuthComConfig>(new Dictionary<string, object>() { { "PartitionKey", "OAuthComConfig" }, { "RowKey",RowKey } });
|
|
|
|
+ if (configs.Any())
|
|
|
|
+ {
|
|
|
|
+ string OAuth_Xkw_AppKey = configs[0].AppKey;
|
|
|
|
+ string OAuth_Xkw_AppSecret = configs[0].AppSecret;
|
|
|
|
+ string OAuth_Xkw_RedirectUrl = configs[0].RedirectUrl;
|
|
|
|
+ string OAuth_Xkw_OAuthHost = configs[0].OAuthHost;
|
|
|
|
+ string OAuth_Xkw_ServiceUrl = configs[0].ServiceUrl;
|
|
|
|
+ var client = new XkwOAuthClient(OAuth_Xkw_AppKey, OAuth_Xkw_AppSecret, OAuth_Xkw_RedirectUrl, OAuth_Xkw_OAuthHost, accessToken, openId, userId);
|
|
|
|
+ client.SERVICE_URL = OAuth_Xkw_ServiceUrl;
|
|
|
|
+ return client;
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|