2009-05-07 4 views
6

パスワードの期限が切れると、ユーザーを[パスワードの変更]ページにリダイレクトする必要があります。リクエストをリダイレクトできる小切手はどこに置く必要がありますか?

このコードを1か所に配置して、任意の要求をパスワード変更ページにリダイレクトすることができます。

AuthorizeAttributeを拡張してOnActionExecutingをオーバーライドしましたが、パスワード変更ページにリダイレクトするルーティングロジックを短絡することはできません。少し明確にするため

、ロジックは次のようになります。

不正な要求:
- >任意のURL - > AuthorizeAttribute - > Login.aspxの - >パスワードの有効期限が切れ - > ChangePassword.aspx

認定要求:
- >任意のURL - >??????? - > ChangePassword.aspx

その?私は何をすべきか分からない部分です。


私はAuthorizeAttributeを拡張するつもりだと思います。私はどこでもそれを使用しますパスワード変更コントローラメソッドを除いて。

答えて

6
public class DenyExpiredPasswordAttribute : AuthorizeAttribute 
{ 

    public override void OnAuthorization(AuthorizationContext filterContext) 
    { 
     IPrincipal user = filterContext.HttpContext.User; 

     if(user != null) 
     { 
      if (user.Identity.IsAuthenticated) 
      { 

       if (CurrentUser.PasswordExpired) // your checking of password expiration 
       { 
        filterContext.HttpContext.Response.Redirect("~/Account/ChangePassword?reason=expired"); 
       } 
      } 
     } 
     base.OnAuthorization(filterContext); 
    } 
} 

これが正常に動作しますが、ちょうどこの属性を持つすべてのコントローラは、「アカウント」1を除外マーク。この方法では、期限切れの属性を持つユーザーはパスワードを変更するまで続行できません。

+1

これは基本的に私が行ったことです。 – Will

+1

これは古い答えなので、(少なくともMVC3の場合) 'filterContext.HttpContext.Response.Redirect("〜/ Account/ChangePassword?reason = expired ");を' filterContext.Result = new RedirectResult( "〜/ Account/ChangePassword?reason = expired"); '(ベース:http://stackoverflow.com/a/2187364/700926およびhttp://stackoverflow.com/a/2765148/700926) –

1

global.asaxでPostAuthenticateRequestイベントのイベントハンドラを追加することができます。

protected void Application_Start(object sender, EventArgs e) { 
    this.PostAuthenticateRequest += new EventHandler(Global_PostAuthenticateRequest); 
} 

void Global_PostAuthenticateRequest(object sender, EventArgs e) 
{ 
if (passwordExpired) { 
    Context.Response.Redirect("~/ChangePassword.aspx"); 
    } 
} 
+0

MVCでそのイベントが放棄されたようですが、明らかにそのイベントが放棄されました。 – Will

関連する問題