2017-08-30 16:49:28 +08:00
|
|
|
|
// ***********************************************************************
|
|
|
|
|
// Assembly : OpenAuth.App
|
|
|
|
|
// Author : yubaolee
|
|
|
|
|
// Created : 07-08-2016
|
|
|
|
|
//
|
|
|
|
|
// Last Modified By : yubaolee
|
|
|
|
|
// Last Modified On : 07-08-2016
|
|
|
|
|
// Contact : Microsoft
|
|
|
|
|
// File: AuthUtil.cs
|
|
|
|
|
// ***********************************************************************
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using Infrastructure;
|
2017-11-30 17:47:41 +08:00
|
|
|
|
using OpenAuth.App.Response;
|
2017-08-30 16:49:28 +08:00
|
|
|
|
|
|
|
|
|
namespace OpenAuth.App.SSO
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>վ<EFBFBD><D5BE>¼<EFBFBD><C2BC>֤<EFBFBD><D6A4>
|
|
|
|
|
/// <para><3E><>¼ʱ<C2BC><CAB1></para>
|
|
|
|
|
/// <code>
|
|
|
|
|
/// var result = AuthUtil.Login(AppKey, username, password);
|
|
|
|
|
/// if (result.Success)
|
|
|
|
|
/// return Redirect("/home/index?Token=" + result.Token);
|
|
|
|
|
/// </code>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class AuthUtil
|
|
|
|
|
{
|
|
|
|
|
static HttpHelper _helper = new HttpHelper(ConfigurationManager.AppSettings["SSOPassport"]);
|
|
|
|
|
|
|
|
|
|
private static string GetToken()
|
|
|
|
|
{
|
|
|
|
|
string token = HttpContext.Current.Request.QueryString["Token"];
|
|
|
|
|
if (!String.IsNullOrEmpty(token)) return token;
|
|
|
|
|
|
|
|
|
|
var cookie = HttpContext.Current.Request.Cookies["Token"];
|
|
|
|
|
return cookie == null ? String.Empty : cookie.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static bool CheckLogin(string token, string remark = "")
|
|
|
|
|
{
|
|
|
|
|
if (String.IsNullOrEmpty(token) || String.IsNullOrEmpty(GetToken()))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var requestUri = String.Format("/api/Check/GetStatus?token={0}&requestid={1}", token, remark);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var value = _helper.Get(null, requestUri);
|
|
|
|
|
return Boolean.Parse(value);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>¼״̬
|
|
|
|
|
/// <para>ͨ<><CDA8>URL<52>е<EFBFBD>Token<65><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Cookie<69>е<EFBFBD>Token</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="remark"><3E><>ע<EFBFBD><D7A2>Ϣ</param>
|
|
|
|
|
public static bool CheckLogin(string remark="")
|
|
|
|
|
{
|
|
|
|
|
return CheckLogin(GetToken(), remark);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0>¼<EFBFBD><C2BC><EFBFBD>û<EFBFBD><C3BB><EFBFBD>Ϣ
|
|
|
|
|
/// <para>ͨ<><CDA8>URL<52>е<EFBFBD>Token<65><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Cookie<69>е<EFBFBD>Token</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="remark">The remark.</param>
|
|
|
|
|
/// <returns>LoginUserVM.</returns>
|
|
|
|
|
public static UserWithAccessedCtrls GetCurrentUser(string remark = "")
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var requestUri = String.Format("/api/Check/GetUser?token={0}&requestid={1}", GetToken(), remark);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var value = _helper.Get<UserWithAccessedCtrls>(null, requestUri);
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD><C7B0>¼<EFBFBD><C2BC><EFBFBD>û<EFBFBD><C3BB><EFBFBD>
|
|
|
|
|
/// <para>ͨ<><CDA8>URL<52>е<EFBFBD>Token<65><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Cookie<69>е<EFBFBD>Token</para>
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="remark">The remark.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
public static string GetUserName(string remark = "")
|
|
|
|
|
{
|
|
|
|
|
var requestUri = String.Format("/api/Check/GetUserName?token={0}&requestid={1}", GetToken(), remark);
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var value = _helper.Get<string>(null, requestUri);
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-10-12 16:38:46 +08:00
|
|
|
|
/// <20><>¼<EFBFBD>ӿ<EFBFBD>
|
2017-08-30 16:49:28 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appKey">Ӧ<>ó<EFBFBD><C3B3><EFBFBD>key.</param>
|
|
|
|
|
/// <param name="username"><3E>û<EFBFBD><C3BB><EFBFBD></param>
|
|
|
|
|
/// <param name="pwd"><3E><><EFBFBD><EFBFBD></param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
public static LoginResult Login(string appKey, string username, string pwd)
|
|
|
|
|
{
|
|
|
|
|
var requestUri = "/api/Check/Login";
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var value = _helper.Post(new
|
|
|
|
|
{
|
|
|
|
|
AppKey = appKey,
|
|
|
|
|
UserName = username,
|
|
|
|
|
Password = pwd
|
|
|
|
|
}, requestUri);
|
|
|
|
|
|
|
|
|
|
var result = JsonHelper.Instance.Deserialize<LoginResult>(value);
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// ע<><D7A2>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool Logout()
|
|
|
|
|
{
|
|
|
|
|
var token = GetToken();
|
|
|
|
|
if (String.IsNullOrEmpty(token)) return true;
|
|
|
|
|
|
|
|
|
|
var requestUri = String.Format("/api/Check/Logout?token={0}&requestid={1}", token, "");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var value = _helper.Post(null, requestUri);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-08 11:28:38 +08:00
|
|
|
|
}
|