mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-04-05 17:38:01 +08:00
131 lines
3.6 KiB
C#
131 lines
3.6 KiB
C#
// ***********************************************************************
|
||
// 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;
|
||
using OpenAuth.App.ViewModel;
|
||
|
||
namespace OpenAuth.App.SSO
|
||
{
|
||
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("/SSO/Check/GetStatus?token={0}&requestid={1}", token, remark);
|
||
|
||
try
|
||
{
|
||
var value = _helper.Get(null, requestUri);
|
||
return bool.Parse(value);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
|
||
public static bool CheckLogin(string remark="")
|
||
{
|
||
return CheckLogin(GetToken(), remark);
|
||
}
|
||
|
||
public static LoginUserVM GetCurrentUser(string remark = "")
|
||
{
|
||
|
||
var requestUri = string.Format("/SSO/Check/GetUser?token={0}&requestid={1}", GetToken(), remark);
|
||
|
||
try
|
||
{
|
||
var value = _helper.Get<LoginUserVM>(null, requestUri);
|
||
return value;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw ex;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20><>½<EFBFBD>ӿ<EFBFBD>
|
||
/// </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 string Login(string appKey, string username, string pwd)
|
||
{
|
||
var requestUri = "/SSO/Login/Check";
|
||
|
||
try
|
||
{
|
||
var value = _helper.Post(new
|
||
{
|
||
AppKey = appKey,
|
||
UserName = username,
|
||
Password = pwd
|
||
}, requestUri);
|
||
|
||
var result = JsonHelper.Instance.Deserialize<LoginResult>(value);
|
||
if (result.Success)
|
||
{
|
||
return result.Token;
|
||
}
|
||
else
|
||
{
|
||
return string.Empty;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return string.Empty;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// ע<><D7A2>
|
||
/// </summary>
|
||
public static bool Logout()
|
||
{
|
||
var token = GetToken();
|
||
if (string.IsNullOrEmpty(token)) return true;
|
||
|
||
var requestUri = string.Format("/SSO/Login/Logout?token={0}&requestid={1}", token, "");
|
||
|
||
try
|
||
{
|
||
var value = _helper.Post(requestUri);
|
||
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
}
|
||
} |