0

アプリでカスタム エンティティを使用してフォーム認証を使用しています。

私が経験している問題は、認証されたセッションが予想される 10 分よりも早く期限切れになることです。

その Cookie に対して認証された要求が来るたびに、Cookie を更新する必要があると思いますか?

セッションを維持するために正しいことはありますか?

ありがとう - 従うべきコード:

Web.Config

<authentication mode="Forms">
  <forms loginUrl="/Admin/Account/LogOn" slidingExpiration="true" />
</authentication>

サインイン時

        var identity = new AppIdentity(member.Fullname, member.Id, roles.ToArray());
        FormsAuthentication.SetAuthCookie(identity.ToString(), false);

        var myCookie = FormsAuthentication.GetAuthCookie(identity.ToString(), false);
        myCookie.Domain = string.Format(".{0}", "mydomain.com");
        HttpContext.Current.Response.AppendCookie(myCookie);

Global.cs

    protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        var cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
        var ticket = TryDecryptCookie(cookie);

        if (ticket == null)
        {
            return;
        }

        var identity = new AppIdentity(ticket);
        var principal = new GenericPrincipal(identity, identity.Roles);

        Context.User = principal;
    }
4

1 に答える 1

0

必要に応じて有効期限を延長するだけで終わりました。

protected void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies[FormsAuthentication.FormsCookieName];
        FormsAuthenticationTicket ticket = TryDecryptCookie(cookie);

        if (ticket == null)
        {
            return;
        }

        FormsAuthenticationTicket newTicket = ticket;

        if (FormsAuthentication.SlidingExpiration)
        {
            newTicket = FormsAuthentication.RenewTicketIfOld(ticket);
        }

        var identity = new AppIdentity(newTicket);
        var principal = new GenericPrincipal(identity, identity.Roles);

        Context.User = principal;
    }
于 2013-04-01T21:30:16.783 に答える