2024-05-08 10:17:02 +08:00
|
|
|
|
using BLL;
|
|
|
|
|
using BLL.Common;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using System.Web.UI;
|
|
|
|
|
using System.Web.UI.WebControls;
|
|
|
|
|
|
|
|
|
|
namespace FineUIPro.Web
|
|
|
|
|
{
|
|
|
|
|
public partial class ssocallback : System.Web.UI.Page
|
|
|
|
|
{
|
|
|
|
|
private string code = string.Empty;
|
|
|
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
this.code = Request.Params["code"];
|
2024-06-07 15:13:19 +08:00
|
|
|
|
BLL.ErrLogInfo.WriteLog($"获取code={this.code}");
|
2024-05-08 10:17:02 +08:00
|
|
|
|
if (string.IsNullOrEmpty(this.code))
|
|
|
|
|
{
|
|
|
|
|
Response.Redirect("~/login.aspx");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-06-03 14:52:50 +08:00
|
|
|
|
var token= GetAccessToken(this.code);
|
|
|
|
|
var userInfo = getUserInfo(token);
|
|
|
|
|
if (userInfo == null)
|
|
|
|
|
{
|
|
|
|
|
Response.Redirect("~/login.aspx");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Response.Redirect("~/index.aspx");
|
2024-05-08 10:17:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private AccessTokenModel GetAccessToken(string _code)
|
|
|
|
|
{
|
2024-06-03 14:52:50 +08:00
|
|
|
|
string clientId = "9379ad91-eef9-4956-a1ee-8b04bb3d42c8";
|
|
|
|
|
string clientSecret = "iLu8Q~4DRYAn~sMjvO1j.tgRERFWhILvLYRPNc9S";
|
2024-05-08 10:17:02 +08:00
|
|
|
|
string grant_type = "authorization_code";
|
2024-06-07 15:13:19 +08:00
|
|
|
|
string redirect_url = "https://fcl-test.basf-ypc.net.cn/ssocallback.aspx";
|
2024-05-08 10:17:02 +08:00
|
|
|
|
string scope = "profile openid";
|
2024-06-07 15:13:19 +08:00
|
|
|
|
string baseUrl = $"https://login.microsoftonline.com/{clientId}/oauth2/v2.0/token";
|
2024-05-08 10:17:02 +08:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string postData = $"code={code}&client_id={clientId}&client_secret={clientSecret}&grant_type={grant_type}&redirect_uri={redirect_url}&scope={scope}";
|
2024-06-07 15:13:19 +08:00
|
|
|
|
BLL.ErrLogInfo.WriteLog($"请求参数postData={postData}");
|
|
|
|
|
string result = BLL.Common.HttpHelper.HttpPost(baseUrl, postData);
|
|
|
|
|
BLL.ErrLogInfo.WriteLog($"请求API Result={result}");
|
2024-05-08 10:17:02 +08:00
|
|
|
|
var Data = JsonConvert.DeserializeObject<AccessTokenModel>(result);
|
|
|
|
|
|
|
|
|
|
return Data;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ErrLogInfo.WriteLog(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-06-03 14:52:50 +08:00
|
|
|
|
private UserTokenModel getUserInfo(AccessTokenModel token)
|
2024-05-08 10:17:02 +08:00
|
|
|
|
{
|
|
|
|
|
string baseUrl = "https://graph.microsoft.com/oidc/userinfo";
|
2024-06-03 14:52:50 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var result = HttpHelper.HttpGetRequest(baseUrl, token.access_token);
|
2024-06-07 15:13:19 +08:00
|
|
|
|
BLL.ErrLogInfo.WriteLog($"获取用户信息:{result}");
|
2024-06-03 14:52:50 +08:00
|
|
|
|
if (result.IndexOf("sub") > -1)
|
|
|
|
|
{
|
|
|
|
|
var info = JsonConvert.DeserializeObject<UserTokenModel>(result);
|
|
|
|
|
//写入session信息
|
2024-06-07 15:13:19 +08:00
|
|
|
|
|
2024-06-03 14:52:50 +08:00
|
|
|
|
//写入cookie信息
|
|
|
|
|
|
|
|
|
|
return info;
|
|
|
|
|
}
|
2024-05-08 10:17:02 +08:00
|
|
|
|
|
2024-06-03 14:52:50 +08:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
//这里报错了,写入日志
|
|
|
|
|
BLL.ErrLogInfo.WriteLog(ex.Message);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2024-05-08 10:17:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2024-06-03 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
public class UserTokenModel
|
|
|
|
|
{
|
|
|
|
|
public string sub { get; set; }
|
|
|
|
|
|
|
|
|
|
public string name { get; set; }
|
|
|
|
|
|
|
|
|
|
public string family_name { get; set; }
|
|
|
|
|
|
|
|
|
|
public string given_name { get; set; }
|
|
|
|
|
|
|
|
|
|
public string picture { get; set; }
|
|
|
|
|
public string email { get; set; }
|
|
|
|
|
|
|
|
|
|
}
|
2024-05-08 10:17:02 +08:00
|
|
|
|
}
|