前段时间用MVC + Redis 做session搞了个简单的单点登录Web站。真是日了狗的问题多。

今天正好睡不着,做个备忘笔记>_<

实现方法很简单,无非就是从重载个Controller或 做一个ActionFilterAttribute就可以达到目的。

下面贴一个Controller的代码实现,ActionFilterAttribute实现方式类似:

这里我为了图方便用了servicestack.redis 虽然最新的免费版本 有很大的性能限制(真坑爹)。

两个Controller 类,Base用于在Action执行前填充的Account信息,一个要求必须登陆,否则调到SSO

PS:可做小修改,例如带Token时二次重定向刷新页面或是将Token存入Cache。

    
/// <summary>
/// 为所有Action填充Account用户信息
/// </summary>
  public class BaseAccountController : Controller
{
protected RedisHelper.RedisHelper redisHelper { get; private set; }
protected readonly static String TokenKeyCookie = "QTMAccountTokenCookie"; public BaseAccountController()
{
redisHelper = new RedisHelper.RedisHelper();
} ~BaseAccountController()
{
if (redisHelper != null)
{
redisHelper.Dispose();
}
} protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var cookie = filterContext.HttpContext.Request.Cookies[TokenKeyCookie];
String token = filterContext.HttpContext.Request.QueryString["Token"]; if (cookie != null)
{
token = cookie.Value;
var accountIndex = redisHelper.Get<AccountIndex>(token);
ViewBag.CurrentAccount = accountIndex;
}
else if (!String.IsNullOrWhiteSpace(token))
{
var accountIndex = CreateTokenCookie(token);
ViewBag.CurrentAccount = accountIndex;
}
} /// <summary>
/// 创建Token令牌的本地Cookie
/// </summary>
/// <param name="Token">Token令牌</param>
/// <returns></returns>
protected AccountIndex CreateTokenCookie(string Token)
{
var accountIndex = redisHelper.Get<AccountIndex>(Token); //判断是否 为有效的Token令牌
if (accountIndex == null)
{
return null;
} //生产Token令牌的Cookie
var token_cookie = new HttpCookie(TokenKeyCookie, Token)
{
HttpOnly = true,
Secure = FormsAuthentication.RequireSSL,
Path = "/",
Expires = DateTime.Now.AddYears()
}; HttpContext.Response.Cookies.Add(token_cookie); return accountIndex;
}

    /// <summary>
/// 控制器的 所有Action必须 持有 有效的 Account Token令牌。
/// </summary>
public class AccountAuthenController : BaseAccountController
{ public AccountAuthenController()
: base()
{
} ~AccountAuthenController()
{
if (redisHelper != null)
{
redisHelper.Dispose();
}
} protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
String token = filterContext.HttpContext.Request.QueryString["Token"];
var cookie = filterContext.HttpContext.Request.Cookies[TokenKeyCookie]; if (cookie == null)
{
if (!String.IsNullOrWhiteSpace(token))
{
//当有返回新的Token令牌时,创建一个新的Token本地Cookie
var accountIndex = CreateTokenCookie(token); if (accountIndex == null)
{
filterContext.HttpContext.Response.Redirect(String.Format(ActionUri.Login, filterContext.HttpContext.Request.Url.AbsoluteUri));
}
}
else
{
filterContext.HttpContext.Response.Redirect(String.Format(ActionUri.Login, filterContext.HttpContext.Request.Url.AbsoluteUri));
}
}
else
{
token = cookie.Value;
var accountIndex = redisHelper.Get<AccountIndex>(token); if (accountIndex == null)
{ //无效的Token Cookie
filterContext.HttpContext.Response.Cookies[TokenKeyCookie].Expires = DateTime.Now.AddYears(-);
filterContext.HttpContext.Response.Redirect(String.Format(ActionUri.Login, filterContext.HttpContext.Request.Url.AbsoluteUri));
}
} base.OnActionExecuting(filterContext);
} #region Private Method #endregion
}

MVC SSO登陆 的麻烦事~的更多相关文章

  1. ASP.NET MVC SSO单点登录设计与实现

    实验环境配置 HOST文件配置如下: 127.0.0.1 app.com127.0.0.1 sso.com IIS配置如下: 应用程序池采用.Net Framework 4.0 注意IIS绑定的域名, ...

  2. ASP.NET MVC SSO单点登录设计与实现(转载)

    实验环境配置 HOST文件配置如下: 127.0.0.1 app.com127.0.0.1 sso.com IIS配置如下: 应用程序池采用.Net Framework 4.0 注意IIS绑定的域名, ...

  3. ASP.NET MVC SSO 单点登录设计与实现

    实验环境配置 HOST文件配置如下: 127.0.0.1 app.com127.0.0.1 sso.com IIS配置如下: 应用程序池采用.Net Framework 4.0 注意IIS绑定的域名, ...

  4. MVC4/5+jquery+bootstrap样式+dataTables+linq+WCF+EF6后台和前台的框架集合!好蛋疼哦!数据库支持MYSQL 和MSSQL,oracle。集成腾讯企业邮箱收邮件同步用户SSO登陆等功能。

    花费了我好多心血,才做出来,下个项目准备用这个框架! 大家有没有做这方面的可以交流一下! 花费了我好多心血,才做出来,下个项目准备用这个框架! 大家有没有做这方面的可以交流一下! 花费了我好多心血,才 ...

  5. Sahi (3) —— 压力测试Load Test以CAS SSO登陆场景为例(103 Tutorial)

    Sahi (3) -- 压力测试Load Test以CAS SSO登陆场景为例(103 Tutorial) jvm版本: 1.8.0_65 sahi版本: Sahi Pro 6.1.0 参考来源: S ...

  6. 新浪微博SSO登陆机制

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  7. MVC基本登陆与验证码功能实现

    一.基本登陆实现与验证码功能实现,该功能是和spring.net功能集合使用的,因为后面要用到验证是否处于登陆状态 1. 先构建一个登陆页面 @{ Layout = null; } <!DOCT ...

  8. .NET MVC中登陆授权过滤器的使用

    1.写个类LoginAuthorityAttribute,继承自AuthorizeAttribute using System; using System.Collections.Generic; u ...

  9. 新浪微博SSO登陆机制(转载)

    原文地址: http://www.cnblogs.com/AloneSword/p/3840548.html 最近在使用sina微博时,经常性交替使用 weibo.com 和 t.sina.cm.cn ...

随机推荐

  1. mac OS.NE开发环境搭建

    合肥程序员群:49313181.    合肥实名程序员群:128131462 (不愿透露姓名和信息者勿加入,申请备注填写姓名+技术+工作年限) Q  Q:408365330     E-Mail:eg ...

  2. 比CMD更强大的命令行WMIC

    先决条件:a. 启动Windows Management Instrumentation服务,开放TCP135端口.b. 本地安全策略的“网络访问: 本地帐户的共享和安全模式”应设为“经典-本地用户以 ...

  3. perl基础:传递hash类型参数

    1 如果是只有一个参数要传,且是hash,最直接想到的办法就是像传其他类型参数一样直接传, 如:   subFuntion(%hash1); 2 如果有多于一个参数要传,这里假设只有一个参数的类型是h ...

  4. php file_get_contents() 用法

    php 需要访问某个网页 <?php $fh= file_get_contents('http://www.baidu.com/'); echo $fh; ?> 知识扩充 file_get ...

  5. 坑备忘error: expected class-name before '{' token

    今日重构之前的代码,修改了命名空间,然后一处派生的子类定义处总是总是报error: expected class-name before '{' token,网上查了查原因,出现这种情况大致有两种情况 ...

  6. F2工作流引擎Web层全新扁平化UI上线

    特点:引入Bootstrap开源UI样式和fontawesome图标集 扁平化样式使用界面更舒服,按钮主题可快速定义更换,对于集成到业主系统UI图标更加丰富. 以下截取部分图片展示,更多请联系作者登录 ...

  7. 当 jquery.unobtrusive-ajax.js 遇上Web API

    最近在熟悉Abp框架,其基于DDD领域驱动设计...前段可以绕过mvc直接调用根据app层动态生成的webapi,有点神奇~,Web API之前有简单接触过,WCF的轻量级版,一般用于做一写开发性的服 ...

  8. Java: IO 学习小结

    源: 键盘 System.in 硬盘 FileStream 内存 ArrayStream 目的: 控制台 System.out 硬盘 FileStream 内存 ArrayStream 处理大文件或者 ...

  9. Practical Malware Analysis里有关inetsim\APATEDNS

    以前从未接触过linux,碰到了许多问题,按步骤: 1\安装VMWARE,安装ubuntu16.04 问题1:之前装的是VM10,装完后没有安装VMTOOLS,我点安装 VMTOOLS,它弹出“简易安 ...

  10. Centos安装桌面环境(一个命令搞定)

    # yum groupinstall "X Window System" "GNOME Desktop Environment"