| 
									
										
										
										
											2024-05-08 10:17:02 +08:00
										 |  |  |  | using BLL; | 
					
						
							|  |  |  |  | using BLL.Common; | 
					
						
							| 
									
										
										
										
											2024-06-12 13:57:52 +08:00
										 |  |  |  | using Microsoft.IdentityModel.Tokens; | 
					
						
							| 
									
										
										
										
											2024-05-08 10:17:02 +08:00
										 |  |  |  | using Newtonsoft.Json; | 
					
						
							| 
									
										
										
										
											2024-06-12 13:57:52 +08:00
										 |  |  |  | using Newtonsoft.Json.Linq; | 
					
						
							| 
									
										
										
										
											2024-05-08 10:17:02 +08:00
										 |  |  |  | using System; | 
					
						
							|  |  |  |  | using System.Linq; | 
					
						
							| 
									
										
										
										
											2024-06-12 13:57:52 +08:00
										 |  |  |  | using System.Text; | 
					
						
							|  |  |  |  | using System.Web.Security; | 
					
						
							| 
									
										
										
										
											2024-05-08 10:17:02 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace FineUIPro.Web | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     public partial class ssocallback : System.Web.UI.Page | 
					
						
							|  |  |  |  |     { | 
					
						
							|  |  |  |  |         private string code = string.Empty; | 
					
						
							|  |  |  |  |         protected void Page_Load(object sender, EventArgs e) | 
					
						
							|  |  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-06-12 13:57:52 +08:00
										 |  |  |  |              | 
					
						
							| 
									
										
										
										
											2024-05-08 10:17:02 +08:00
										 |  |  |  |             this.code = Request.Params["code"]; | 
					
						
							| 
									
										
										
										
											2024-06-18 10:34:42 +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-12 13:57:52 +08:00
										 |  |  |  |             var token =  GetAccessToken(this.code); | 
					
						
							| 
									
										
										
										
											2024-06-03 14:52:50 +08:00
										 |  |  |  |             var userInfo = getUserInfo(token); | 
					
						
							|  |  |  |  |             if (userInfo == null) | 
					
						
							|  |  |  |  |             { | 
					
						
							|  |  |  |  |                 Response.Redirect("~/login.aspx"); | 
					
						
							|  |  |  |  |                 return; | 
					
						
							|  |  |  |  |             } | 
					
						
							| 
									
										
										
										
											2024-06-11 11:36:17 +08:00
										 |  |  |  |              | 
					
						
							| 
									
										
										
										
											2024-06-03 14:52:50 +08:00
										 |  |  |  |             Response.Redirect("~/index.aspx"); | 
					
						
							| 
									
										
										
										
											2024-05-08 10:17:02 +08:00
										 |  |  |  |         } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |         private AccessTokenModel GetAccessToken(string _code) | 
					
						
							|  |  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-06-18 10:34:42 +08:00
										 |  |  |  |             string clientId = Funs.ClientId; | 
					
						
							|  |  |  |  |             string clientSecret = Funs.ClientSecret; | 
					
						
							|  |  |  |  |             string redirect_url = Funs.Redirect_url; | 
					
						
							| 
									
										
										
										
											2024-05-08 10:17:02 +08:00
										 |  |  |  |             string grant_type = "authorization_code"; | 
					
						
							|  |  |  |  |             string scope = "profile openid"; | 
					
						
							| 
									
										
										
										
											2024-06-11 11:36:17 +08:00
										 |  |  |  |             string baseUrl = $"https://login.microsoftonline.com/ecaa386b-c8df-4ce0-ad01-740cbdb5ba55/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-18 10:34:42 +08:00
										 |  |  |  |                 //BLL.ErrLogInfo.WriteLog($"请求参数postData={postData}"); | 
					
						
							| 
									
										
										
										
											2024-06-07 15:13:19 +08:00
										 |  |  |  |                 string result = BLL.Common.HttpHelper.HttpPost(baseUrl, postData); | 
					
						
							| 
									
										
										
										
											2024-06-18 10:34:42 +08:00
										 |  |  |  |                 //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-11 11:36:17 +08:00
										 |  |  |  |         private Model.Sys_User getUserInfo(AccessTokenModel token) | 
					
						
							| 
									
										
										
										
											2024-05-08 10:17:02 +08:00
										 |  |  |  |         { | 
					
						
							| 
									
										
										
										
											2024-06-03 14:52:50 +08:00
										 |  |  |  |             try | 
					
						
							|  |  |  |  |             { | 
					
						
							| 
									
										
										
										
											2024-06-11 11:36:17 +08:00
										 |  |  |  |                 string username = string.Empty; | 
					
						
							| 
									
										
										
										
											2024-06-12 13:57:52 +08:00
										 |  |  |  |                 string[] toke_split= token.id_token.Split('.'); | 
					
						
							|  |  |  |  |                 var header = Encoding.UTF8.GetString(Base64UrlEncoder.DecodeBytes(toke_split[0])); | 
					
						
							|  |  |  |  |                 var clamis= Encoding.UTF8.GetString(Base64UrlEncoder.DecodeBytes(toke_split[1])); | 
					
						
							| 
									
										
										
										
											2024-06-18 10:34:42 +08:00
										 |  |  |  |                 //BLL.ErrLogInfo.WriteLog("clamis=" + clamis); | 
					
						
							| 
									
										
										
										
											2024-06-12 13:57:52 +08:00
										 |  |  |  |                 JObject jo = JObject.Parse(clamis); | 
					
						
							|  |  |  |  |                 if (jo["cn"] != null) | 
					
						
							| 
									
										
										
										
											2024-06-03 14:52:50 +08:00
										 |  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2024-06-12 13:57:52 +08:00
										 |  |  |  |                     username = jo["cn"].ToString(); | 
					
						
							| 
									
										
										
										
											2024-06-03 14:52:50 +08:00
										 |  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-06-12 13:57:52 +08:00
										 |  |  |  |                 else | 
					
						
							|  |  |  |  |                 { | 
					
						
							|  |  |  |  |                     if (jo["preferred_username"] != null) | 
					
						
							| 
									
										
										
										
											2024-06-11 11:36:17 +08:00
										 |  |  |  |                     { | 
					
						
							| 
									
										
										
										
											2024-06-12 13:57:52 +08:00
										 |  |  |  |                         string preferred_username = jo["preferred_username"].ToString(); | 
					
						
							|  |  |  |  |                         username = preferred_username.Split('@')[0]; | 
					
						
							|  |  |  |  |                     }    | 
					
						
							| 
									
										
										
										
											2024-06-11 11:36:17 +08:00
										 |  |  |  |                 } | 
					
						
							|  |  |  |  |                 var info = Funs.DB.Sys_User.Where(t => t.Account == username && t.IsPost == true).FirstOrDefault(); | 
					
						
							|  |  |  |  |                 if (info != null) | 
					
						
							|  |  |  |  |                 { | 
					
						
							| 
									
										
										
										
											2024-06-12 13:57:52 +08:00
										 |  |  |  |                     FormsAuthentication.SetAuthCookie(username, false); | 
					
						
							| 
									
										
										
										
											2024-06-11 11:36:17 +08:00
										 |  |  |  |                     Session[SessionName.CurrUser] = info; | 
					
						
							|  |  |  |  |                 } | 
					
						
							|  |  |  |  |                | 
					
						
							|  |  |  |  |                 return info; | 
					
						
							| 
									
										
										
										
											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
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-08 10:17:02 +08:00
										 |  |  |  | } |