增加SSO逻辑

This commit is contained in:
yubaolee
2016-07-08 11:28:38 +08:00
parent d3c98fdc87
commit 032bc20e1e
112 changed files with 52068 additions and 178 deletions

View File

@@ -0,0 +1,76 @@
using System;
using System.Configuration;
using System.Web;
using Infrastructure;
namespace OpenAuth.App.SSO
{
public class AuthUtil
{
static HttpHelper _helper = new HttpHelper(ConfigurationManager.AppSettings["SSOPassport"]);
public static bool CheckLogin(string token, string remark = "")
{
var requestUri = string.Format("/api/Passport?token={0}&requestid={1}", token, remark);
try
{
var value = _helper.Get(null, requestUri);
return bool.Parse(value);
}
catch (Exception ex)
{
throw ex;
}
}
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;
}
}
public static bool Logout()
{
var tokenCookie = HttpContext.Current.Request.Cookies["Token"];
if (tokenCookie == null) return true;
string token = tokenCookie.Value;
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;
}
}
}
}

View File

@@ -0,0 +1,10 @@
namespace OpenAuth.App.SSO
{
public class LoginResult
{
public bool Success;
public string ErrorMsg;
public string ReturnUrl;
public string Token;
}
}

View File

@@ -0,0 +1,68 @@
using System.Web;
using System.Web.Mvc;
namespace OpenAuth.App.SSO
{
public class SSOAuthAttribute : ActionFilterAttribute
{
public const string Token = "Token";
public const string SessionUserName = "SessionUserName";
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var token = "";
var cookieSessionUserName = "";
//Token by QueryString
var request = filterContext.HttpContext.Request;
if (request.QueryString[Token] != null)
{
token = request.QueryString[Token];
filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(Token, token));
}
else if (request.Cookies[Token] != null) //从Cookie读取Token
{
token = request.Cookies[Token].Value;
}
//SessionUserName by QueryString
if (request.QueryString[SessionUserName] != null)
{
cookieSessionUserName = request.QueryString[SessionUserName];
filterContext.HttpContext.Response.Cookies.Add(new HttpCookie(SessionUserName, cookieSessionUserName));
}
else if (request.Cookies[SessionUserName] != null) //从Cookie读取SessionUserName
{
cookieSessionUserName = request.Cookies[SessionUserName].Value;
}
if (string.IsNullOrEmpty(token))
{
//直接登录
filterContext.Result = SsoLoginResult(cookieSessionUserName);
}
else
{
//验证
if (AuthUtil.CheckLogin(token, request.RawUrl) == false)
{
//会话丢失,跳转到登录页面
filterContext.Result = SsoLoginResult(cookieSessionUserName);
}
}
base.OnActionExecuting(filterContext);
}
private static ActionResult SsoLoginResult(string username)
{
//跳转到SSO站点登陆
//return new RedirectResult(string.Format("{0}/sso/login?appkey={1}&username={2}",
// ConfigurationManager.AppSettings["SSOPassport"],
// ConfigurationManager.AppSettings["SSOAppKey"],
// username));
return new RedirectResult("/Login/Index");
}
}
}