[HttpPost] public ActionResult CheckLogin(User model) { if (!ModelState.IsValid) return null; string userData=new JavaScriptSerializer().Serialize(model); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,model.LoginName,DateTime.Now,DateTime.Now.AddMinutes(2),false,userData); string cookieValue = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,cookieValue); HttpContext.Response.Cookies.Add(cookie); //var userData = new MyUserDataPrincipal { UserId=1}; //MyFormsAuthentication<MyUserDataPrincipal>.SetAuthCookie("s", userData, true); return View(); }
以上是发送cookie的代码
protected void Application_PostAuthenticateRequest(object sender, System.EventArgs e) { HttpCookieCollection cookieCollection= HttpContext.Current.Request.Cookies; string [] cookieArray=cookieCollection.AllKeys; HttpCookie cookie = cookieCollection.Get(FormsAuthentication.FormsCookieName); }
以上是接收请求的cookie的代码 得到的cookie的值 永远是null 拿不到cookie
下面MVC最外层的web.config代码
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" /> </sectionGroup> </configSections> <system.web.webPages.razor> <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> <pages pageBaseType="System.Web.Mvc.WebViewPage"> <namespaces> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Optimization"/> <add namespace="System.Web.Routing" /> </namespaces> </pages> </system.web.webPages.razor> <appSettings> <add key="webpages:Enabled" value="false" /> </appSettings> <system.web> <authentication mode="Forms"> <forms loginUrl="~/Login/Index2" timeout="2880" /> </authentication> <httpHandlers> <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/> </httpHandlers> <!-- 在视图页面中启用请求验证将导致验证在 控制器已对输入进行处理后发生。默认情况下, MVC 在控制器处理输入前执行请求验证。 若要更改此行为,请对控制器或操作 应用 ValidateInputAttribute。 --> <pages validateRequest="false" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <controls> <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" /> </controls> </pages> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <handlers> <remove name="BlockViewHandler"/> <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> </handlers> </system.webServer> </configuration>
只能帮到这里,剩下的靠你自己调试
感谢兄弟 后来我搞出来了 其实是小细节错了。 Return View(); 里面没参数 默认去查找CheckLogin这个视图 ,而这个视图我没添加 所以这cookie根本没返回到页面上。细节问题 自己害死自己了 。还是得感谢你的回答 让我对调戏又有了更深的理解!