public ActionResult Login(string data)
{
var _params = JsonConvert.DeserializeAnonymousType(data, new { userName = "", password = "" });
string userIdMd5 = _params.userName.Md5Sign();//查询UserId,需加密
string token = Guid.NewGuid().ToString();//token,用于加密
if (RedisHelper.Get(userIdMd5) == null)//写入缓存
{
RedisHelper.Set(userIdMd5, new { token, _params.userName, _params.password }, TimeSpan.FromMinutes());
}
else
{
token = JsonConvert.DeserializeAnonymousType(RedisHelper.Get(userIdMd5), new { token }).token;
}
Response.Cookies.Add(new HttpCookie("userIdMd5", userIdMd5));
return Json(new { token });//返回Token
}
using cpf360.Common;
using cpf360.DTO;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc; namespace HanLiPrj.Filter
{
public class NeedLoginAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if (!httpContext.Request.Cookies.AllKeys.Contains("userIdMd5") || RedisHelper.Get(httpContext.Request.Cookies["userIdMd5"].Value) == null)
{
httpContext.Response.Write(JsonConvert.SerializeObject(new OutputData { code=, message = "请登录" }));
return false;
} string userInfo = RedisHelper.Get(httpContext.Request.Cookies["userIdMd5"].Value);
string token = JsonConvert.DeserializeAnonymousType(userInfo, new { token = "" }).token;
if (!httpContext.Request.QueryString.AllKeys.Contains("sign") || !httpContext.Request.QueryString.AllKeys.Contains("timespan"))
{
httpContext.Response.Write(JsonConvert.SerializeObject(new OutputData { code = , message = "请加权" }));
return false;
}
if ((DateTime.Now - httpContext.Request.QueryString["timespan"].ToDateTime()).TotalSeconds > )
{
httpContext.Response.Write(JsonConvert.SerializeObject(new OutputData { code = , message = "请求超时" }));
return false;
}
string method = httpContext.Request.HttpMethod;
string data = "";
if (method == "GET")
{
IDictionary<string, string> parameters = new Dictionary<string, string>();
for (int f = ; f < httpContext.Request.QueryString.AllKeys.Count(); f++)
{
string key = httpContext.Request.QueryString.AllKeys[f];
if (key == "sign") continue;
parameters.Add(key, httpContext.Request.QueryString[key]);
} // 第二步:把字典按Key的字母顺序排序
IDictionary<string, string> sortedParams = new SortedDictionary<string, string>(parameters);
IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator(); // 第三步:把所有参数名和参数值串在一起
StringBuilder query = new StringBuilder();
while (dem.MoveNext())
{
string key = dem.Current.Key;
string value = dem.Current.Value;
if (!string.IsNullOrEmpty(key))
{
query.Append(key).Append(value);
}
}
data = query.ToString();
}
else if (method == "POST")
{
data = httpContext.Request.Form["data"] + httpContext.Request.QueryString["timespan"];
}
var md5String = (data + token).Md5Sign();
if (md5String != httpContext.Request.QueryString["sign"])
{
httpContext.Response.Write(JsonConvert.SerializeObject(new OutputData { code = , message = "请加权" }));
return false;
} RedisHelper.Remove(httpContext.Request.Cookies["userIdMd5"].Value);//清除缓存
RedisHelper.Set(httpContext.Request.Cookies["userIdMd5"].Value, userInfo, TimeSpan.FromMinutes());//延长缓存时间
return true;
}
}
}

MVC 登陆鉴权的更多相关文章

  1. axios interceptors 拦截 , 页面跳转, token 验证 Vue+axios实现登陆拦截,axios封装(报错,鉴权,跳转,拦截,提示)

    Vue+axios实现登陆拦截,axios封装(报错,鉴权,跳转,拦截,提示) :https://blog.csdn.net/H1069495874/article/details/80057107 ...

  2. shiro 实现 用户 a 操作b 的权限 ,用户 b 能够及时获知。b不需要退出登陆 。 关闭鉴权缓存,或者不配置缓存

    <bean id="myRealm" class="com.diancai.util.MyRealm"> <property name=&qu ...

  3. ApiAuthValue鉴权机制总结

    一.背景介绍 1.自动化的配置工具autoconfig介绍 项目开发过程中,有些配置会随着运行环境的变化而各不相同.如jdbc驱动的配置,在开发环境可能链接到开发本地的数据库,测试环境则有一套测试专用 ...

  4. 搭建一个分布式MongoDB鉴权集群

    今天休假在家,测试并搭建了一个replica set shard MongoDB鉴权集群.replica set shard 鉴权集群中文资料比较少,本文是个人笔记,同时也希望对后来者有所帮助.本文仅 ...

  5. 关于springmvc 方法注解拦截器的解决方案,多用于方法的鉴权

    最近在用SpringMvc写项目的时候,遇到一个问题,就是方法的鉴权问题,这个问题弄了一天了终于解决了,下面看下解决方法 项目需求:需要鉴权的地方,我只需要打个标签即可,比如只有用户登录才可以进行的操 ...

  6. spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法

    spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法 前言 本篇接着<spring boot / cloud ...

  7. Docker mongodb 3.4 分片 一主 一副 一仲 鉴权集群部署.

    非docker部署 为了避免过分冗余,并且在主节点挂了,还能顺利自动提升,所以加入仲裁节点 为什么要用docker部署,因为之前直接在虚拟机启动10个mongod 进程.多线程并发测试的时候,mong ...

  8. mongodb 3.4 分片 一主 一副 一仲 鉴权集群部署.

    Docker方式部署 为了避免过分冗余,并且在主节点挂了,还能顺利自动提升,所以加入仲裁节点 mongodb版本: 环境:一台虚拟机 三个configsvr 副本: 端口为 27020,27021,2 ...

  9. SpringBoot系列: Web应用鉴权思路

    ==============================web 项目鉴权============================== 主要的鉴权方式有:1. 用户名/密码鉴权, 然后通过 Sess ...

随机推荐

  1. 1111 Online Map

    题意:给定一个图,以及起点和终点,需要我们计算两条路径.第1条路径:距离最短路径,若不唯一,则选择用时最短的那一条:第2条路径:用时最少路径,若不唯一,选择经过结点数最少的那一条. 思路:两次Dijk ...

  2. SQL语句替换字段内容例子

    SELECT REPLACE(SysFuncID,'7201','7204') ,           CASE           WHEN Flag=1 THEN REPLACE(funcname ...

  3. 2014.8.27 Vs2005宏的使用

    终于知道怎么像在Word里那样使用宏了! 1.vs2005必须装补丁1 2.在C:\Program Files (x86)\Common Files\microsoft shared\VSA\8.0\ ...

  4. C# IP地址去掉端口号

    string Ip1 = "192.168.0.199:7777"; string Ip2 = Ip1.Remove(Ip1.IndexOf(':'));

  5. 第十二章 MySQL触发器(待续)

    ······

  6. JAVA的FileOutput/InputStream使用实例

    在JAVA中,要读写文件,要使用Stream这个东西. Stream简单来说,可以看做在程序和文件之间打开了一个管道,然后把数据通过这个管道输送到文件或程序中去. FileOutput/InputSt ...

  7. 【转】VS 安全开发生命周期(SDL)检查

    前面在学习使用google的protobuf时在VS2012中一直无法编译编译通过,经过查找一些资料原来发现,并不是protobuf的问题,而是自己在使用VS2012时,没有完全了解VS2012的强大 ...

  8. 缓存数据库redis

    什么是Redis? Redis是一个TCP服务器,支持请求/响应协议. 在Redis中,请求通过以下步骤完成: 客户端向服务器发送查询,并从套接字读取,通常以阻塞的方式,用于服务器响应. 服务器处理命 ...

  9. jQuery+css模拟下拉框模糊搜索的实现

    html: @*输入框*@ <div> <input type="text" style="width: 85%; height: 34px;" ...

  10. Catalan数计算及应用

    Catalan数列是非常奇妙的一列数字,因为很多问题的解就是一个Catalan数.知道了这一规律,很多看似复杂的问题便可迎刃而解.那么什么是Catalan数,什么样的问题的解是Catalan数呢? 1 ...