开发者

authentication cookie

开发者 https://www.devze.com 2023-01-09 16:43 出处:网络
Using Asp.Net MVC 1, I have my \"log on\" control/page...I check the \"remember me\" checkbox and hit submit.. in my controller I have:

Using Asp.Net MVC 1, I have my "log on" control/page...I check the "remember me" checkbox and hit submit.. in my controller I have:

FormsAuth.SignIn(userName, password, rememberMe)

This method creates the persisted cookie .ASPXAUTH and everything is good at this point.. I put a breakpoint in other controller, and I noticed that once I'm loggued to the website.. for the next "postback" or page refresh... the cookie .ASPXAUTH has gone from the cookies collection... so.. when I'm back to the site, even when I selected the option "remember me".. it ask my credentials again with the login form...

any idea why this is happening?


UPDATED:

I think this is not a problem with MVC. I created a new application with WebForms, the page con开发者_StackOverflow社区tains 1 textbox, 1 button to generate the cookie.. and other button to write the cookie in a label.. http://screencast.com/t/MjQ1MTVmYWU

this is my code:

protected void Button1_Click(object sender, EventArgs e)
{
    HttpCookie cookie = new HttpCookie("test");
    cookie.Value = TextBox1.Text;
    cookie.Expires = DateTime.Now.AddMinutes(1);

    Response.Cookies.Add(cookie);
}//with a breakpoint here, I "watch" the Response.Cookies collection and I can see my "test" item there.

protected void Button2_Click(object sender, EventArgs e)
{//with a breakpoint here. the Response.Cookies collection is empty.
    Label1.Text = Response.Cookies["test"].Value ?? "null";
}

I'm having the same result.. the cookie is added correctly to the Response.Cookies collection, but in the second postback the collection is empty again..

the result.. the label gets the value "null".

maybe some configuration? I have never worked before with cookies.


As I said before this is the first time I work with cookies...

the problem was that..for retreiving the value I have to use the collection Request.Cookies instead of Response.Cookies... this sounds reasonable because the request is sent from the browser.. (my fault xP)

so, in summary...

  1. Response.Cookies to create cookies
  2. the cookie is stored in the browser
  3. Request.Cookies to retrieve the cookie's value

Thanks everybody!

0

精彩评论

暂无评论...
验证码 换一张
取 消