mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2026-04-17 19:18:03 +08:00
v1.2版,全面实现SSO登陆
This commit is contained in:
@@ -1,17 +1,42 @@
|
||||
// ***********************************************************************
|
||||
// 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 = "")
|
||||
{
|
||||
|
||||
var requestUri = string.Format("/api/Passport?token={0}&requestid={1}", token, remark);
|
||||
if (string.IsNullOrEmpty(token) || string.IsNullOrEmpty(GetToken()))
|
||||
return false;
|
||||
|
||||
var requestUri = string.Format("/SSO/Check/GetStatus?token={0}&requestid={1}", token, remark);
|
||||
|
||||
try
|
||||
{
|
||||
@@ -24,6 +49,34 @@ namespace OpenAuth.App.SSO
|
||||
}
|
||||
}
|
||||
|
||||
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";
|
||||
@@ -53,12 +106,14 @@ namespace OpenAuth.App.SSO
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ע<><D7A2>
|
||||
/// </summary>
|
||||
public static bool Logout()
|
||||
{
|
||||
var tokenCookie = HttpContext.Current.Request.Cookies["Token"];
|
||||
if (tokenCookie == null) return true;
|
||||
var token = GetToken();
|
||||
if (string.IsNullOrEmpty(token)) return true;
|
||||
|
||||
string token = tokenCookie.Value;
|
||||
var requestUri = string.Format("/SSO/Login/Logout?token={0}&requestid={1}", token, "");
|
||||
|
||||
try
|
||||
|
||||
86
OpenAuth.App/SSO/SSOController.cs
Normal file
86
OpenAuth.App/SSO/SSOController.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
// ***********************************************************************
|
||||
// Assembly : OpenAuth.Mvc
|
||||
// Author : Administrator
|
||||
// Created : 09-22-2015
|
||||
//
|
||||
// Last Modified By : Administrator
|
||||
// Last Modified On : 09-22-2015
|
||||
// ***********************************************************************
|
||||
// <copyright file="BaseController.cs" company="">
|
||||
// Copyright (c) . All rights reserved.
|
||||
// </copyright>
|
||||
// <summary>
|
||||
// 基础控制器
|
||||
// 继承该控制器可以防止未登录查看
|
||||
// 继承该控制器后,如果想访问控制器中存在,但模块配置里面没有的Action(如:Home/Git),请使用AnonymousAttribute
|
||||
// </summary>
|
||||
// ***********************************************************************
|
||||
|
||||
using System.Web;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
public class SSOController : Controller
|
||||
{
|
||||
public const string Token = "Token";
|
||||
public const string SessionUserName = "SessionUserName";
|
||||
|
||||
protected 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");
|
||||
}
|
||||
}
|
||||
}
|
||||
20
OpenAuth.App/SSO/UserAuthSession.cs
Normal file
20
OpenAuth.App/SSO/UserAuthSession.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
|
||||
namespace OpenAuth.WebApi.Areas.SSO.Models
|
||||
{
|
||||
[Serializable]
|
||||
public class UserAuthSession
|
||||
{
|
||||
public string Token { get; set; }
|
||||
|
||||
public string AppKey { get; set; }
|
||||
|
||||
public string UserName { get; set; }
|
||||
|
||||
public string IpAddress { get; set; }
|
||||
|
||||
public DateTime InvalidTime { get; set; }
|
||||
|
||||
public DateTime CreateTime { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user