0

私は実際に私が利用しているものを次の投稿に従っています。

https://stackoverflow.com/a/15939899/1118485

ご覧のとおり、これは のメソッドの 1 つにのみ実装されていますHomeController。コードの書き直しを避けるために、BaseController.

    protected new ViewResult View()
    {
        if (Session["sessionid"] == null )
        {
            //Session["sessionid"] = "empty";
            return base.View();
        }

        // check to see if your ID in the Logins table has LoggedIn = true - if so, continue, otherwise, redirect to Login page.
        if (OperationContext.IsYourLoginStillTrue(System.Web.HttpContext.Current.User.Identity.Name, Session["sessionid"].ToString()))
        {
            // check to see if your user ID is being used elsewhere under a different session ID
            if (!OperationContext.IsUserLoggedOnElsewhere(System.Web.HttpContext.Current.User.Identity.Name, Session["sessionid"].ToString()))
            {
                //return base.View();
            }
            else
            {
                // if it is being used elsewhere, update all their Logins records to LoggedIn = false, except for your session ID
                OperationContext.LogEveryoneElseOut(System.Web.HttpContext.Current.User.Identity.Name, Session["sessionid"].ToString());
                //return base.View();
            }
        }
        else
        {
            FormsAuthentication.SignOut();
            Attention("You have logged out because another user with the account has been connnected.");
            //return RedirectToAction("Login", "Account");
        }

        return base.View();
    }

しかし、ご覧のとおり、これはViewメソッドを使用する場合にのみ機能します。他のコントローラーでは、いくつかのまたはを使用しましRedirectsFileResult。したがって、すべての ActionResult が実行される必要があります。上記のコードを確認してください。ActionResultAttribute調査中に、すべてのコントローラーにカスタムを実装する必要があると思いましたが、そうですか?

私が正しければ、カスタムを使用してActionResultAttr常にログインを確認する方法についてのデモ実装を見せてもらえますか。または、私が間違っている場合、どうすればよいですか?

4

1 に答える 1

0

アクションの前に実行される Controller クラスの OnAuthorization メソッドを使用できます。

public class BaseController : Controller
{
    protected override void OnAuthorization(AuthorizationContext filterContext)
    {
        base.OnAuthorization(filterContext);

        if ( /*session is not valid*/)
        {
            filterContext.Result = RedirectToAction("SessionExpired", "Account");
        }
    }
}
于 2013-06-01T23:42:21.450 に答える