• 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(20));
                }
                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=1, 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 = 1, message = "请加权" }));
                    return false;
                }
                if ((DateTime.Now - httpContext.Request.QueryString["timespan"].ToDateTime()).TotalSeconds > 300)
                {
                    httpContext.Response.Write(JsonConvert.SerializeObject(new OutputData { code = 1, message = "请求超时" }));
                    return false;
                }
                string method = httpContext.Request.HttpMethod;
                string data = "";
                if (method == "GET")
                {
                    IDictionary<string, string> parameters = new Dictionary<string, string>();
                    for (int f = 0; 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 = 1, message = "请加权" }));
                    return false;
                }
    
                RedisHelper.Remove(httpContext.Request.Cookies["userIdMd5"].Value);//清除缓存
                RedisHelper.Set(httpContext.Request.Cookies["userIdMd5"].Value, userInfo, TimeSpan.FromMinutes(20));//延长缓存时间
                return true;
            }
        }
    }
  • 相关阅读:
    左除与右除的区别--MATLAB
    【FPGA】 007 --Verilog中 case,casez,casex的区别
    Spring Boot企业级博客系统实战视频教程
    Java对象的序列化和反序列化
    消息队列ActiveMQ的使用详解
    消息队列深入解析
    面试中关于Redis的问题看这篇就够了
    一文轻松搞懂redis集群原理及搭建与使用
    渣渣的实习春招总结
    淘淘商城项目补充(5)查询,删除,更新内容列表功能的实现
  • 原文地址:https://www.cnblogs.com/chenyishi/p/8876044.html
Copyright © 2020-2023  润新知