2017-07-28 74 views
1

所以我一直在这个工作了很长一段时间,似乎无法找到一个有用的答案。我正在创建一个包含刷新标记字符串的cookie。添加后,我可以在我的Request.Cookies中看到它被添加,但它并未显示在开发者控制台中,我的程序似乎无法找到它。我添加一个cookie,我看到它添加,然后它不再有那

这里是我的创作和添加是,当他们成功验证RAN:

Response.Cookies.Remove("RefreshToken"); 
FormsAuthenticationTicket tick = new FormsAuthenticationTicket(1, 
    response.AppUser.Username, 
    DateTime.Now, 
    DateTime.Now.AddSeconds(response.AppUser.ExpiresIn), 
    true, 
    response.AppUser.AccessToken); 

string encrypt = FormsAuthentication.Encrypt(tick); 

HttpCookie cookie = new HttpCookie("RefreshToken", encrypt); 
cookie.Value = response.AppUser.RefreshToken; 
cookie["RemembeMe"] = rememberMe.ToString(); 
cookie.Secure = true; 
cookie.Name = "RefreshToken"; 
cookie.Path = Request.ApplicationPath; 
cookie.Expires = DateTime.Now.AddMinutes(15); 
Response.Cookies.Add(cookie); 

下面是我检索它在Global.asax时,我的身份验证令牌在请求开始时届满:

if (HttpContext.Current.Request.Cookies["RefreshToken"] != null) 
{ 
    var z = HttpContext.Current.Request.Cookies["RefreshToken"].Value; 
    Resources.ReauthorizeUser rau = new Resources.ReauthorizeUser();  
} 

下面是一些我的web.config中:

<sessionState cookieless="false" regenerateExpiredSessionId="false"/> 
<customErrors mode="Off"/> 
<authentication mode="Forms"> 
    <forms loginUrl="~/Account/Login" timeout="5" protection="All" cookieless="UseCookies" slidingExpiration="false"/> 
</authentication> 

设我知道如果我错过了任何重要的代码。任何帮助都会令人惊叹,这一直在推动着我的坚强。

+1

在请求管道中的哪个点执行添加cookie的代码?有没有其他代码可以设置该cookie或覆盖它?您在浏览器的开发控制台中看到了哪些cookie?请求管道中的哪一点是您检索cookie? – Igor

+0

啊,我知道我会忽略一些重要的信息。好问题,我会在上面更新。哦,我没有回答有关开发控制台,它从来没有出现在那里。 – Darkwingduck

+1

看起来像是将cookie值设置为字符串encrypt(在HttpCookie构造函数中),但是将值覆盖为response.AppUser.RefreshToken。 –

回答

2

只有通过SSL提出请求时才会发送您的Cookie。 如果您不使用SSL,请尝试使用cookie.Secure = false;

+0

嗯所以是的我猜本地它不会是SSL? – Darkwingduck

+2

除非你专门设定了这一点,否则我不这么认为。 –

+0

好吧,谢谢,我会尽快尝试 – Darkwingduck

相关问题