140 lines
5.9 KiB
C#
140 lines
5.9 KiB
C#
using Model;
|
|
using Newtonsoft.Json.Linq;
|
|
using RestSharp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BLL.Oauth
|
|
{
|
|
public class OauthService: IOauthService
|
|
{
|
|
string CNCECOA_Url = Funs.CNCECOA_Url;
|
|
string CNCECOA_Client_id = Funs.CNCECOA_Client_id;
|
|
string CNCECOA_Client_secret = Funs.CNCECOA_Client_secret;
|
|
public string GetAccessToken(string code)
|
|
{
|
|
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
|
|
string token = "";
|
|
if (string.IsNullOrEmpty(code)) { return token; }
|
|
string baseUrl = CNCECOA_Url + "/idp/oauth2/getToken?client_id=" + CNCECOA_Client_id + "&grant_type=authorization_code&client_secret=" + CNCECOA_Client_secret + "&code=" + code;
|
|
var client = new RestClient(baseUrl);
|
|
client.Timeout = -1;
|
|
var request = new RestRequest(Method.POST);
|
|
IRestResponse response = client.Execute(request);
|
|
//Console.WriteLine(response.Content);
|
|
JObject jt = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(response.Content);
|
|
token = jt["access_token"].ToString();
|
|
return token;
|
|
}
|
|
/// <summary>
|
|
/// 统一门户集成-根据token获取用户信息
|
|
/// </summary>
|
|
/// <param name="AccessToken"></param>
|
|
/// <returns></returns>
|
|
public UserInfo GetUserInfo(string AccessToken)
|
|
{
|
|
try
|
|
{
|
|
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
|
|
Model.UserInfo userInfo = null;
|
|
if (string.IsNullOrEmpty(AccessToken)) { return userInfo; }
|
|
string baseurl = string.Format(CNCECOA_Url + "/idp/oauth2/getUserInfo?access_token={0}&client_id={1}", AccessToken, CNCECOA_Client_id);
|
|
var client = new RestClient(baseurl);
|
|
client.Timeout = -1;
|
|
var request = new RestRequest(Method.GET);
|
|
IRestResponse response = client.Execute(request);
|
|
APICommonService.SaveSysHttpLog("Oauth_GetUserInfo", baseurl, response.Content);
|
|
userInfo = UserInfo.FromJson(response.Content);
|
|
return userInfo;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrLogInfo.WriteLog("统一门户获取用户信息失败", ex);
|
|
return null;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 匹配qhse用户信息
|
|
/// </summary>
|
|
/// <param name="userInfo"></param>
|
|
/// <param name="message"></param>
|
|
/// <returns></returns>
|
|
public Model.Sys_User GetLoginInfo(UserInfo userInfo, ref string message)
|
|
{
|
|
Sys_User sys_User = new Model.Sys_User();
|
|
if (!string.IsNullOrEmpty(userInfo.IdentityNumber)) //先匹配身份证号对应用户
|
|
{
|
|
sys_User = BLL.UserService.GetUserByIdentityCard(userInfo.IdentityNumber);
|
|
if (sys_User == null)
|
|
{
|
|
sys_User = new Sys_User();
|
|
sys_User.Account = userInfo.LoginName;
|
|
sys_User.UserName = userInfo.DisplayName;
|
|
sys_User.Password = Funs.EncryptionPassword(UserService.getInitialPassword("",userInfo.IdentityNumber));
|
|
sys_User.IsPost = true;
|
|
sys_User.IdentityCard = userInfo.IdentityNumber;
|
|
sys_User.Telephone = userInfo.Mobile;
|
|
UserService.AddUser(sys_User);
|
|
|
|
}
|
|
return sys_User;
|
|
}
|
|
else if (!string.IsNullOrEmpty(userInfo.Mobile))//再匹配手机号对应用户
|
|
{
|
|
sys_User = BLL.UserService.GetUserByMobile(userInfo.Mobile);
|
|
if (sys_User == null)
|
|
{
|
|
sys_User = new Sys_User();
|
|
sys_User.Account = userInfo.LoginName;
|
|
sys_User.UserName = userInfo.DisplayName;
|
|
sys_User.Password = Funs.EncryptionPassword(UserService.getInitialPassword("", userInfo.IdentityNumber));
|
|
sys_User.IsPost = true;
|
|
sys_User.IdentityCard = userInfo.IdentityNumber;
|
|
sys_User.Telephone = userInfo.Mobile;
|
|
UserService.AddUser(sys_User);
|
|
|
|
}
|
|
return sys_User;
|
|
}/*
|
|
else//最后从主数据拉取用户信息
|
|
{
|
|
var person = BLL.MasterDataService.GetMasterdataPersonByCode(userInfo.EmployeeNumber);
|
|
if (person != null)
|
|
{
|
|
sys_User = BLL.UserService.GetUserByIdentityCard(person.CertNum);
|
|
if (sys_User == null)
|
|
{
|
|
sys_User = new Sys_User();
|
|
sys_User.Account = userInfo.LoginName;
|
|
sys_User.UserName = userInfo.DisplayName;
|
|
sys_User.Password = Funs.EncryptionPassword(UserService.getInitialPassword("", userInfo.IdentityNumber));
|
|
sys_User.IsPost = true;
|
|
sys_User.IdentityCard = person.CertNum;
|
|
sys_User.Telephone = person.PhoneNum;
|
|
|
|
UserService.AddUser(sys_User);
|
|
|
|
}
|
|
return sys_User;
|
|
}
|
|
else
|
|
{
|
|
message = "未找到主数据数据";
|
|
}
|
|
}*/
|
|
return sys_User;
|
|
|
|
}
|
|
|
|
public Model.Sys_User GetLoginInfo(string code, ref string message)
|
|
{
|
|
var userInfo = GetUserInfo(GetAccessToken(code));
|
|
var user= GetLoginInfo(userInfo, ref message);
|
|
return user;
|
|
}
|
|
}
|
|
}
|