質問

MVC.NETのカスタムログインページに取り組んでいます。私はそうするようにログインをチェックします:

public bool Login(string login, string password, bool persistent)
{
  var loginEntity = this.AdminRepository.GetLogin(login, password);
  if (loginEntity != null)
  {
    FormsAuthentication.SetAuthCookie(login, persistent);

    HttpContext.Current.Session["AdminId"] = loginEntity.AdminId;
    HttpContext.Current.Session["AdminUsername"] = loginEntity.Username;

  return true;
  }

次に、フィルター属性を使用して管理者アクセスが必要なコントローラーを飾ります。

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
  var ctx = HttpContext.Current;

  // check if session is supported
  if (ctx.Session != null)
  {
    var redirectTargetDictionary = new RouteValueDictionary();

    // check if a new session id was generated
    if (ctx.Session.IsNewSession)
    {
        // If it says it is a new session, but an existing cookie exists, then it must
        // have timed out
        string sessionCookie = ctx.Request.Headers["Cookie"];
        if (((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) || null == sessionCookie)
        {
          redirectTargetDictionary = new RouteValueDictionary();
          redirectTargetDictionary.Add("area", "Admin");
          redirectTargetDictionary.Add("action", "LogOn");
          redirectTargetDictionary.Add("controller", "Home");

          filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
        }
      } else if (SessionContext.AdminId == null) {
        redirectTargetDictionary = new RouteValueDictionary();
        redirectTargetDictionary.Add("area", "Admin");
        redirectTargetDictionary.Add("action", "LogOn");
        redirectTargetDictionary.Add("controller", "Home");

        filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
      }
    }
    base.OnActionExecuting(filterContext);
}

ログイン後、2つのCookieがあることがわかります。

  1. ASPXAUTH(有効期限が「セッションの終了時」に設定されています(持続する場合は虚偽です)
  2. およびASP.NET_SESSIONID有効期限は常に「セッションの終了時」である

質問:問題は、私が「持続」オプションにtrueを設定したとしても(Aspxauthの有効期限を30分から設定する - これが良いです)、私のセッション[「adminid」]は、ブラウザを閉じて再開した後、常にnullです。セッション(セッション["adminid"]およびセッション["adminusername"])が、最初に「持続」を行うと、Cookieから引き込まれ、Browswerウィンドウを再開することを確認するにはどうすればよいですか。ありがとう

役に立ちましたか?

解決 2

ここで私の解決策を見つけました:私自身のロギングシステムに.aspxauthを使用することは可能ですか?

そして、これは私がしたことです:

    public class SessionExpireFilterAttribute : ActionFilterAttribute
{
    /// <summary>
    /// Controller action filter is used to check whether the session is still active. If the session has expired filter redirects to the login screen.
    /// </summary>
    /// <param name="filterContext"></param>
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var ctx = HttpContext.Current;

        // check if session is supported
        if (ctx.Session != null)
        {
            // check if a new session id was generated
            if (ctx.Session.IsNewSession)
            {
                var identity = ctx.User.Identity;

                // If it says it is a new session, but an existing cookie exists, then it must
                // have timed out
                string sessionCookie = ctx.Request.Headers["Cookie"];
                if (((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0)) || null == sessionCookie)
                {
                    var redirectTargetDictionary = new RouteValueDictionary();
                    redirectTargetDictionary.Add("area", string.Empty);
                    redirectTargetDictionary.Add("action", "LogOn");
                    redirectTargetDictionary.Add("controller", "User");

                    filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
                }

                // Authenticated user, load session info
                else if (identity.IsAuthenticated)
                {
                    var loginRepository = new LoginRepository(InversionOfControl.Container.Resolve<IDbContext>());
                    IAuthenticationService authenticationService = new AuthenticationService(loginRepository);
                    authenticationService.SetLoginSession(identity.Name);
                }
            }
            else if (SessionContext.LoginId == null)
            {
                var redirectTargetDictionary = new RouteValueDictionary();
                redirectTargetDictionary.Add("area", string.Empty);
                redirectTargetDictionary.Add("action", "LogOn");
                redirectTargetDictionary.Add("controller", "User");

                filterContext.Result = new RedirectToRouteResult(redirectTargetDictionary);
            }
        }
        base.OnActionExecuting(filterContext);
    }
}

他のヒント

有効期限のあるクッキーがディスクに書き込まれます。したがって、Cookieが期限切れになっていない場合、ユーザーは次回ブラウザを開くときにログインします。

セッションクッキーはメモリにのみ保存され、ブラウザが閉じられるとすぐに紛失します。

セッションクッキーは、有効期限のないクッキーです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top