使用javascript获取AspNet.ApplicationCookie

时间:2014-04-28 08:58:07

标签: javascript .net cookies

我不知道这可能,但我不得不问。有没有办法从Cookies获得AspNet.ApplicationCookie。

我试过了:

`$.cookie('.AspNet.ApplicationCookie');`

`document.cookie;`

document.cookie.AspNet.ApplicationCookie;

希望有人知道:D

1 个答案:

答案 0 :(得分:0)

您应该能够访问它,但您必须在cookie身份验证选项中明确允许它。对我来说,这通常意味着在App_Start文件夹中打开Startup.Auth.cs并在CookieHttpOnly对象中将false选项设置为CookieAuthenticationOptions。在使用模板的新MVC 5应用程序中,它应如下所示:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{          
    CookieHttpOnly = false, // this option is necessary to allow JavaScript access
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),                       
    Provider = new CookieAuthenticationProvider
    {                    
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))                       
    },                
});