mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-09-18 17:48:01 +08:00
增加SSO逻辑
This commit is contained in:
@@ -41,6 +41,11 @@ namespace OpenAuth.App
|
||||
throw new HttpException(401,"未登录");
|
||||
}
|
||||
string username = HttpContext.Current.User.Identity.Name;
|
||||
return GetLoginUser(username);
|
||||
}
|
||||
|
||||
public LoginUserVM GetLoginUser(string username)
|
||||
{
|
||||
_service.GetUserAccessed(username);
|
||||
var user = new LoginUserVM
|
||||
{
|
||||
@@ -48,11 +53,13 @@ namespace OpenAuth.App
|
||||
AccessedOrgs = _service.Orgs,
|
||||
Modules = _service.Modules.MapToList<ModuleView>(),
|
||||
Resources = _service.Resources,
|
||||
Token = GenerateId.GetGuidHash()
|
||||
};
|
||||
|
||||
foreach (var moduleView in user.Modules)
|
||||
{
|
||||
moduleView.Elements = _service.ModuleElements.Where(u => u.ModuleId == moduleView.Id).OrderBy(u => u.Sort).ToList();
|
||||
moduleView.Elements =
|
||||
_service.ModuleElements.Where(u => u.ModuleId == moduleView.Id).OrderBy(u => u.Sort).ToList();
|
||||
}
|
||||
|
||||
return user;
|
||||
|
@@ -33,9 +33,38 @@
|
||||
<Prefer32Bit>false</Prefer32Bit>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Web.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Web.Helpers, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.Helpers.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.Razor.3.2.3\lib\net45\System.Web.Razor.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.WebPages, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.WebPages.Deployment, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Deployment.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebPages.3.2.3\lib\net45\System.Web.WebPages.Razor.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
@@ -50,6 +79,9 @@
|
||||
<Compile Include="ResourceManagerApp.cs" />
|
||||
<Compile Include="RevelanceManagerApp.cs" />
|
||||
<Compile Include="RoleManagerApp.cs" />
|
||||
<Compile Include="SSO\AuthUtil.cs" />
|
||||
<Compile Include="SSO\LoginResult.cs" />
|
||||
<Compile Include="SSO\SSOAuthAttribute.cs" />
|
||||
<Compile Include="StockManagerApp.cs" />
|
||||
<Compile Include="UserManagerApp.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
@@ -70,6 +102,9 @@
|
||||
<Name>OpenAuth.Domain</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup />
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
|
76
OpenAuth.App/SSO/AuthUtil.cs
Normal file
76
OpenAuth.App/SSO/AuthUtil.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
10
OpenAuth.App/SSO/LoginResult.cs
Normal file
10
OpenAuth.App/SSO/LoginResult.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace OpenAuth.App.SSO
|
||||
{
|
||||
public class LoginResult
|
||||
{
|
||||
public bool Success;
|
||||
public string ErrorMsg;
|
||||
public string ReturnUrl;
|
||||
public string Token;
|
||||
}
|
||||
}
|
68
OpenAuth.App/SSO/SSOAuthAttribute.cs
Normal file
68
OpenAuth.App/SSO/SSOAuthAttribute.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
@@ -22,6 +22,7 @@ namespace OpenAuth.App.ViewModel
|
||||
/// </summary>
|
||||
public class LoginUserVM
|
||||
{
|
||||
public string Token { get; set; }
|
||||
public User User { get; set; }
|
||||
/// <summary>
|
||||
/// 用户可以访问到的模块(包括所属角色与自己的所有模块)
|
||||
|
Reference in New Issue
Block a user