こんにちは、フォーム認証を使用してチケットを作成し、それをレスポンスに追加するとうまくいきます。
Firefox tools>pageinfo>security>cookies によって作成された Cookie を見ると、有効期限が現在 Cookie に設定されていることがわかります。
ローカルでは問題なく動作しますが、サーバー (サーバー 2008-iis7) にアップロードすると、有効期限は機能しませんが、Cookie に設定され、常に約 10 マイナスで Cookie が期限切れになり、メンバーはログアウトします。いくつかの特別な設定があります。インターネットでいくつかの例を見ていますが、何も見つかりません。
私の認証コード:
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
username,//username
DateTime.Now,
DateTime.Now.AddMinutes(120),
true,
rollname,
FormsAuthentication.FormsCookiePath);
string hashCookies = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,hashCookies); // Hashed ticket
cookie.Expires = DateTime.Now.AddMinutes(120);
Response.Cookies.Add(cookie);
リクエストの認証を確認するために、global.asax でこのメソッドを使用しました。
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
// look if any security information exists for this request
if (System.Web.HttpContext.Current.User != null)
{
// see if this user is authenticated, any authenticated cookie (ticket) exists for this user
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
// see if the authentication is done using FormsAuthentication
if (System.Web.HttpContext.Current.User.Identity is FormsIdentity)
{
// Get the roles stored for this request from the ticket
// get the identity of the user
FormsIdentity identity = (FormsIdentity)System.Web.HttpContext.Current.User.Identity;
// get the forms authetication ticket of the user
FormsAuthenticationTicket ticket = identity.Ticket;
// get the roles stored as UserData into the ticket
string[] roles = ticket.UserData.Split(',');
// create generic principal and assign it to the current request
System.Web.HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(identity, roles);
}
}
}
}
私のウェブ設定で:
<authentication mode="Forms">
<forms loginUrl="~/Home.aspx" timeout="120" />
</authentication>