Web API 设置Cookie 以及 Session 问题
1: 设置Cookin 方法一
请参考我的另一篇: Connkin 方法一
2: 设置Cookin 方法二
//Cookie 的设置读取 HttpCookie coo = HttpContext.Current.Request.Cookies["BF"]; if (coo == null) { coo = new HttpCookie("BF"); coo["User.ITCode"] = "张三"; coo["CurrentLanguage"] = "Chinese"; coo.Expires = DateTime.Now.AddDays(7.0); HttpContext.Current.Request.Cookies.Remove("BF"); HttpContext.Current.Response.Cookies.Remove("BF"); HttpContext.Current.Request.Cookies.Add(coo); HttpContext.Current.Response.Cookies.Add(coo); }
3: API 设置Session 问题,
API 在默认的情况下是不开启Session的,所以我们需要重写配置,否则我们在HttpContext.Current.Session 使用的时候,返回的都是NULL。
第一步: 从写 SessionRouteHandler
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http.WebHost; using System.Web.Routing; using System.Web.SessionState; namespace BF.API { public class SessionRouteHandler : HttpControllerHandler, IRequiresSessionState { public SessionRouteHandler(RouteData routeData) : base(routeData) { } } }
第二步: 从写 SessionControllerRouteHandler
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http.WebHost; using System.Web.Routing; namespace BF.API { public class SessionControllerRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { return new SessionRouteHandler(requestContext.RouteData); } } }
第三步:配置路由WebApiConfig
using BFKR.API.Cookie; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Http.Cors; using System.Web.Routing; namespace BFKR.API { public static class WebApiConfig { public static void Register(HttpConfiguration config) { //跨域配置 config.EnableCors(new EnableCorsAttribute("*", "*", "*")); //webApi路由 config.MapHttpAttributeRoutes(); RouteTable.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ).RouteHandler = new SessionControllerRouteHandler(); //移除xml返回格式数据 GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); //配置返回的时间类型数据格式 GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add( new Newtonsoft.Json.Converters.IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" } ); } } }
第四步:配置Global
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Http; using System.Web.Mvc; using System.Web.Routing; using System.Web.Security; using System.Web.SessionState; namespace BFKR.API { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); } public override void Init() { this.PostAuthenticateRequest += (y, z) => HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required); base.Init(); } protected void Session_Start(object sender, EventArgs e) { } protected void Session_End(object sender, EventArgs e) { } protected void Application_BeginRequest(object sender, EventArgs e) { } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { } protected void Application_End(object sender, EventArgs e) { } } }
第五步: 这个时候就可以设置Session了
//Session 的读取 string SESSION_KEY = "BF_Session"; string name = "李四"; //赋值 HttpContext.Current.Session[SESSION_KEY] = name; //设置过期时间 HttpContext.Current.Session.Timeout = 30; //读取 string namestr = HttpContext.Current.Session[SESSION_KEY] as string; //都是删除 HttpContext.Current.Session.Remove(SESSION_KEY); HttpContext.Current.Session[SESSION_KEY] = null;