[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();
}
以上是發(fā)送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);
}
以上是接收請(qǐng)求的cookie的代碼? 得到的cookie的值 永遠(yuǎn)是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>
<!--
在視圖頁(yè)面中啟用請(qǐng)求驗(yàn)證將導(dǎo)致驗(yàn)證在
控制器已對(duì)輸入進(jìn)行處理后發(fā)生。默認(rèn)情況下,
MVC 在控制器處理輸入前執(zhí)行請(qǐng)求驗(yàn)證。
若要更改此行為,請(qǐng)對(duì)控制器或操作
應(yīng)用 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>
?
?
- 1 回答
- 0 關(guān)注
- 928 瀏覽
添加回答
舉報(bào)
0/150
提交
取消