MVC 登陆鉴权
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 登陆鉴权的更多相关文章
- axios interceptors 拦截 , 页面跳转, token 验证 Vue+axios实现登陆拦截,axios封装(报错,鉴权,跳转,拦截,提示)
Vue+axios实现登陆拦截,axios封装(报错,鉴权,跳转,拦截,提示) :https://blog.csdn.net/H1069495874/article/details/80057107 ...
- shiro 实现 用户 a 操作b 的权限 ,用户 b 能够及时获知。b不需要退出登陆 。 关闭鉴权缓存,或者不配置缓存
<bean id="myRealm" class="com.diancai.util.MyRealm"> <property name=&qu ...
- ApiAuthValue鉴权机制总结
一.背景介绍 1.自动化的配置工具autoconfig介绍 项目开发过程中,有些配置会随着运行环境的变化而各不相同.如jdbc驱动的配置,在开发环境可能链接到开发本地的数据库,测试环境则有一套测试专用 ...
- 搭建一个分布式MongoDB鉴权集群
今天休假在家,测试并搭建了一个replica set shard MongoDB鉴权集群.replica set shard 鉴权集群中文资料比较少,本文是个人笔记,同时也希望对后来者有所帮助.本文仅 ...
- 关于springmvc 方法注解拦截器的解决方案,多用于方法的鉴权
最近在用SpringMvc写项目的时候,遇到一个问题,就是方法的鉴权问题,这个问题弄了一天了终于解决了,下面看下解决方法 项目需求:需要鉴权的地方,我只需要打个标签即可,比如只有用户登录才可以进行的操 ...
- spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法
spring boot / cloud (十四) 微服务间远程服务调用的认证和鉴权的思考和设计,以及restFul风格的url匹配拦截方法 前言 本篇接着<spring boot / cloud ...
- Docker mongodb 3.4 分片 一主 一副 一仲 鉴权集群部署.
非docker部署 为了避免过分冗余,并且在主节点挂了,还能顺利自动提升,所以加入仲裁节点 为什么要用docker部署,因为之前直接在虚拟机启动10个mongod 进程.多线程并发测试的时候,mong ...
- mongodb 3.4 分片 一主 一副 一仲 鉴权集群部署.
Docker方式部署 为了避免过分冗余,并且在主节点挂了,还能顺利自动提升,所以加入仲裁节点 mongodb版本: 环境:一台虚拟机 三个configsvr 副本: 端口为 27020,27021,2 ...
- SpringBoot系列: Web应用鉴权思路
==============================web 项目鉴权============================== 主要的鉴权方式有:1. 用户名/密码鉴权, 然后通过 Sess ...
随机推荐
- Windows10 官方原版镜像下载途径 Label:win10解决方案
https://www.microsoft.com/en-gb/software-download/windows10ISO 设置浏览标签为手机以避免跳转,下载即可 或者手机打开该网址,获取下载链接 ...
- 配置MapReduce插件时,弹窗报错org/apache/hadoop/eclipse/preferences/MapReducePreferencePage : Unsupported major.minor version 51.0(Hadoop2.7.3集群部署)
原因: hadoop-eclipse-plugin-2.7.3.jar 编译的jdk版本和eclipse启动使用的jdk版本不一致导致. 解决方案一: 修改myeclipse.ini文件即可解决. ...
- 360良心制作fonts.useso.com
我们的网站,经常会用到google的一些数据. 但在国内无法使用google, 360这个良心的企业,解决了这个问题. 把google替换成useso就可以了. 比如, <link href=& ...
- [置顶]
STM32的ADC1采集多条通道,可以不使用DMA功能吗?
类似的问题 为什么我采集5条通道的电压,而采集到的值却都是第一条的呢? 我什么时候需要使用DMA功能? Ⅰ关于ADC的一些知识 STM32的ADC是一种12位逐次逼近型的模拟数字转换器.它有多达18条 ...
- mongoDB的了解
一.什么是mongoDB? 1.MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统.在高负载的情况下,添加更多的节点,可以保证服务器性能. 2.MongoDB 旨在为WEB ...
- 02-26 ASP.NET加密解密的方法
MD5加密.解密的方法. 使用时的代码备忘:Response.Write(FormsAuthentication.HashPasswordForStoringInConfigFile("要加 ...
- 由ffmpeg中avformat模块中的 URL_SCHEME_CHARS 看 strspn( ) 的妙用
在ffmpeg的avformat 模块中avio.c 对 URL_SCHEME_CHARS 的定义: #define URL_SCHEME_CHARS \ "abcdefghijklmnop ...
- 【HDU4307】Matrix
本篇博客基本全篇转自https://www.cnblogs.com/staginner/archive/2012/08/13/2636826.html,太强啦ORZ 题意 A是个1*n的矩阵,每个元素 ...
- validate 不校验的解决办法
最近使用validate.js用于项目的表单校验,调了半天,怎么都不校验,仔细对照例子发现 例子中要校验的文本框除了ID外还有name属性,而我在做的时候没有name属性,到这里终于 看到了希望,把所 ...
- AutoHotKey 使用ADODB读取Excel 报ADODB.Connection 未找到提供程序,可能未提供
一.系统环境 操作系统:Win7 64位 英文版 Office: Office 2010 64位/32位 AutoHotKey:AutoHotKey 1.1.26.01 二.问题现象 安装了A ...