v1.2版,全面实现SSO登陆

This commit is contained in:
yubaolee
2016-07-08 18:51:48 +08:00
parent 032bc20e1e
commit 781ae8900d
19 changed files with 326 additions and 147 deletions

View File

@@ -4,6 +4,7 @@ using System.Web;
using Infrastructure;
using OpenAuth.App.ViewModel;
using System.Web.Security;
using OpenAuth.App.SSO;
using OpenAuth.Domain.Service;
namespace OpenAuth.App
@@ -17,31 +18,14 @@ namespace OpenAuth.App
_service = service;
}
public void Login(string userName, string password)
{
_service.Check(userName, password);
FormsAuthentication.SetAuthCookie(userName, true);
}
/// <summary>
/// 开发者登陆
/// </summary>
public void LoginByDev()
{
_service.SetSysUser();
FormsAuthentication.SetAuthCookie("System", true);
}
public LoginUserVM GetLoginUser()
{
if (!HttpContext.Current.User.Identity.IsAuthenticated)
if (!AuthUtil.CheckLogin())
{
throw new HttpException(401,"未登录");
}
string username = HttpContext.Current.User.Identity.Name;
return GetLoginUser(username);
return AuthUtil.GetCurrentUser();
}
public LoginUserVM GetLoginUser(string username)
@@ -53,7 +37,6 @@ namespace OpenAuth.App
AccessedOrgs = _service.Orgs,
Modules = _service.Modules.MapToList<ModuleView>(),
Resources = _service.Resources,
Token = GenerateId.GetGuidHash()
};
foreach (var moduleView in user.Modules)

View File

@@ -2,6 +2,7 @@
using OpenAuth.Domain;
using System.Collections.Generic;
using System.Web;
using OpenAuth.App.SSO;
using OpenAuth.Domain.Service;
namespace OpenAuth.App
@@ -20,8 +21,7 @@ namespace OpenAuth.App
/// </summary>
public dynamic Load(int parentId, int pageindex, int pagesize)
{
string loginuser = HttpContext.Current.User.Identity.Name;
return _moduleManService.Load(loginuser, parentId, pageindex, pagesize);
return _moduleManService.Load(AuthUtil.GetCurrentUser().User.Account, parentId, pageindex, pagesize);
}
public void Delete(int id)

View File

@@ -80,8 +80,10 @@
<Compile Include="RevelanceManagerApp.cs" />
<Compile Include="RoleManagerApp.cs" />
<Compile Include="SSO\AuthUtil.cs" />
<Compile Include="SSO\SSOController.cs" />
<Compile Include="SSO\LoginResult.cs" />
<Compile Include="SSO\SSOAuthAttribute.cs" />
<Compile Include="SSO\UserAuthSession.cs" />
<Compile Include="StockManagerApp.cs" />
<Compile Include="UserManagerApp.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

View File

@@ -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

View 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>
// 基础控制器
// 继承该控制器可以防止未登录查看
// 继承该控制器后如果想访问控制器中存在但模块配置里面没有的ActionHome/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");
}
}
}

View 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; }
}
}

View File

@@ -22,7 +22,6 @@ namespace OpenAuth.App.ViewModel
/// </summary>
public class LoginUserVM
{
public string Token { get; set; }
public User User { get; set; }
/// <summary>
/// 用户可以访问到的模块(包括所属角色与自己的所有模块)