• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
灬伊天?
博客园    首页    新随笔    联系   管理    订阅  订阅

006-Session登录(Cookie)

  1 ---AuthorizePlusAttribute.cs---
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Web;
  6 using System.Web.Mvc;
  7 using System.Web.SessionState;
  8 
  9 namespace Session.Filters
 10 {
 11     public class AuthorizePlusAttribute : AuthorizeAttribute, IRequiresSessionState
 12     {
 13         public override void OnAuthorization(AuthorizationContext filterContext)
 14         {
 15             if (Convert.ToBoolean(filterContext.HttpContext.Session["SessionId"]))
 16             {
 17                 //验证成功
 18             }
 19             else
 20             {
 21                 //验证失败(返回401)
 22                 base.HandleUnauthorizedRequest(filterContext);
 23             }
 24         }
 25     }
 26 }
 27 ---Json-Login.ashx.cs---
 28 using MySQL.Account;
 29 using MySQL.Models.Account;
 30 using System;
 31 using System.Collections.Generic;
 32 using System.Linq;
 33 using System.Web;
 34 using System.Web.SessionState;
 35 
 36 namespace Json.Submit.User
 37 {
 38     /// <summary>
 39     /// Json_Login
 40     /// </summary>
 41     public class Json_Login : IHttpHandler, IRequiresSessionState
 42     {
 43         ORM_Admini admini = new ORM_Admini();
 44         public void ProcessRequest(HttpContext context)
 45         {
 46             context.Response.ContentType = "text/plain";
 47             string user = context.Request["post_user"];
 48             string pass = context.Request["post_pass"];
 49             List<Admini> list = admini.GetLogin(user, pass);
 50             if (list.Count == 0)
 51             {
 52                 context.Response.Write("False");
 53             }
 54             else
 55             {
 56                 context.Session["SessionId"] = true;
 57                 context.Session.Timeout = 10;
 58                 context.Response.Cookies["SessionName"].Value = list[0].Nickname;
 59                 context.Response.Cookies["SessionName"].Expires = DateTime.Now.AddMinutes(10);
 60             }
 61             //System.Web.HttpContext.Current.Session["userName"] = "管理员";
 62             //HttpContext.Current.Session.Timeout = 1;
 63             //HttpContext.Current.Response.Cookies["Id"].Value = HttpContext.Current.Session.SessionID.ToString();
 64             //HttpContext.Current.Response.Cookies["Id"].Expires = DateTime.Now.AddMilliseconds(2000);
 65             //HttpContext.Current.Response.Cookies["Name"].Value = "ning";
 66             //HttpContext.Current.Response.Cookies["Name"].Expires = DateTime.Now.AddMilliseconds(2000);
 67             //HttpContext.Current.Response.Cookies["age"].Value = "18";
 68             //HttpContext.Current.Response.Cookies["age"].Expires = DateTime.Now.AddMilliseconds(2000);
 69         }
 70 
 71         public bool IsReusable
 72         {
 73             get
 74             {
 75                 return false;
 76             }
 77         }
 78     }
 79 }
 80 ---Json-Quit.ashx.cs---
 81 using System;
 82 using System.Collections.Generic;
 83 using System.Linq;
 84 using System.Web;
 85 using System.Web.SessionState;
 86 
 87 namespace Json.Submit.User
 88 {
 89     /// <summary>
 90     /// Json_Quit
 91     /// </summary>
 92     public class Json_Quit : IHttpHandler, IRequiresSessionState
 93     {
 94 
 95         public void ProcessRequest(HttpContext context)
 96         {
 97             context.Response.ContentType = "text/plain";
 98             string post = context.Request["post_Quit"];
 99             if (post == "Quit")
100             {
101                 System.Web.HttpContext.Current.Session["SessionId"] = false;
102                 context.Response.Write("False");
103             }
104         }
105 
106         public bool IsReusable
107         {
108             get
109             {
110                 return false;
111             }
112         }
113     }
114 }

 

posted @ 2018-09-08 16:51  灬伊天☂  阅读(166)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3