私の mvc 4 アプリケーションでは、他の情報を使用して認証 Cookie を作成しています。データが 1KB 未満であれば問題なく動作しますが、1KB を超えると Cookie が作成されません。
私の知る限り、Cookie の最大サイズは apx 4KB です。私のコードは以下です。
if (result.Status == ActionStatus.Success)
{
AuctionSiteApplication = result.Data;
CreateCustomAuthorisationCookie(AuctionSiteApplication.User.Email, obj.RememberMe, new JavaScriptSerializer().Serialize(AuctionSiteApplication));
if ((AuctionSiteApplication.User.UserType == UserType.SUAdmin) || (AuctionSiteApplication.User.UserType == UserType.Admin))
{
return RedirectToAction("Index", "Dashboard", new { area = "Admin" });
}
else
{
return RedirectToAction("Index", "Home", new { area = "" });
}
}
protected void CreateCustomAuthorisationCookie(String user_name, Boolean is_persistent, String custom_data)
{
FormsAuthenticationTicket auth_ticket =
new FormsAuthenticationTicket(
1, user_name,
DateTime.Now,
DateTime.Now.AddMinutes(30),
is_persistent, custom_data, ""
);
String encrypted_ticket_ud = FormsAuthentication.Encrypt(auth_ticket);
HttpCookie auth_cookie_ud = new HttpCookie(Cookies.UserCookie, encrypted_ticket_ud);
if (is_persistent) auth_cookie_ud.Expires = auth_ticket.Expiration;
System.Web.HttpContext.Current.Response.Cookies.Add(auth_cookie_ud);
}
protected override void OnAuthorization(AuthorizationContext filter_context)
{
if (Request.RawUrl.ToLower().Contains("www.")) filter_context.Result = RedirectPermanent(Request.RawUrl.ToLower().Replace("www.", ""));
HttpCookie auth_cookie = Request.Cookies[Cookies.UserCookie];
#region If auth cookie is present
if (auth_cookie != null)
{
FormsAuthenticationTicket auth_ticket = FormsAuthentication.Decrypt(auth_cookie.Value);
AuctionSiteApplication = new JavaScriptSerializer().Deserialize<AuctionSiteApplication>(auth_ticket.UserData);
System.Web.HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(new FormsIdentity(auth_ticket), null);
ViewBag.AuctionSiteApplication = AuctionSiteApplication;
base.OnAuthorization(filter_context);
}
#endregion
// Rest Code
...
}
以下は、保存されていないCookieに保存しようとしているデータです
{"User":{"UserID":1,"Email":"abctest@pqr.com","FirstName":"abc","LastName":"Arora","UserType":2,"UserCompanies":[{"CompanyId":35,"CompanyName":"New Company","CompanyRoleId":96,"IsAdmin":true},{"CompanyId":36,"CompanyName":"tryrtyr","CompanyRoleId":103,"IsAdmin":true},{"CompanyId":37,"CompanyName":"abc","CompanyRoleId":109,"IsAdmin":false},{"CompanyId":35,"CompanyName":"New Company","CompanyRoleId":98,"IsAdmin":false},{"CompanyId":37,"CompanyName":"abc","CompanyRoleId":109,"IsAdmin":false},{"CompanyId":37,"CompanyName":"abc","CompanyRoleId":109,"IsAdmin":false},{"CompanyId":37,"CompanyName":"abc","CompanyRoleId":109,"IsAdmin":false},{"CompanyId":37,"CompanyName":"abc","CompanyRoleId":109,"IsAdmin":false},{"CompanyId":37,"CompanyName":"abc","CompanyRoleId":109,"IsAdmin":false},{"CompanyId":36,"CompanyName":"tryrtyr","CompanyRoleId":105,"IsAdmin":false}],"IsAuthenticated":true},"Company":{"CompanyId":0,"CompanyName":null,"CompanyRoleId":96,"IsAdmin":true}}
以下は、適切に保存されているCookieに保存しようとしているデータです
{"User":{"UserID":2,"Email":"abc@pqr.com","FirstName":"abc","LastName":"Arora","UserType":1,"UserCompanies":[{"CompanyId":35,"CompanyName":"New Company","CompanyRoleId":0,"IsAdmin":false},{"CompanyId":36,"CompanyName":"tryrtyr","CompanyRoleId":0,"IsAdmin":false},{"CompanyId":37,"CompanyName":"abc","CompanyRoleId":0,"IsAdmin":false}],"IsAuthenticated":true},"Company":{"CompanyId":0,"CompanyName":"SUAdmin","CompanyRoleId":2,"IsAdmin":false}}