2009-07-15 18 views
3

コントローラ上でAuthorizeとRequireSSL(MVC先物)属性の両方を正しく使用している人はいますか?私は、ユーザーがログインし、実行するために安全な接続を使用する必要があるというルールを強制しなければならないコントローラを作成しました。ユーザーが安全な接続をしていない場合は、アプリをhttpsにリダイレクトする必要があるため、RequireSSL属性でRedirect = trueを使用しています。コードのようなもの(CheckPasswordExpiredは私の自家製の属性です)になります。ASP.NET MVC先物RequireSSL属性と属性を一緒に承認

[Authorize] 
[RequireSsl(Redirect = true)] 
[CheckPasswordExpired(ActionName = "ChangePassword", 
    ControllerName = "Account")] 
[HandleError] 
public class ActionsController : Controller 
{ 
    .... 
} 

mysite.com/Actions/Indexをフォーム認証のためにリダイレクトするサイトと、デフォルトのページのデフォルトルートです。

私がhttp://mysite.comにアクセスしたときに、ユーザーが安全な接続にリダイレクトされ、ログインページにまだ認証されていないために取得したいものがあります。私が得るのは、HTTP 400エラー(Bad Request)です。 http://mysite.com/Account/Loginに移動すると、リダイレクトは機能しますが、アカウントコントローラもログインアクションも[承認]属性を持ちません。

誰もが私の目的を達成するためにこれらの2つの属性を一緒に使用する経験がありますか?

ありがとうございます!

答えて

4

私は両方とも成功しています。あなたのデフォルトアクションに属性がありますか?

私は世界的にSSLを無効にすることができるようにところで、私は先物よりも少し変更したバージョンを使用してい
public class HomeController : BaseController 
{ 
    [Authorize] 
    [RequireSsl] 
    public ActionResult Index() 
    { 
    } 
} 

:返信用

[AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)] 
public sealed class RequireSslAttribute : FilterAttribute, IAuthorizationFilter 
{ 
    public RequireSslAttribute() 
    { 
     Redirect = true; 
    } 

    public bool Redirect { get; set; } 

    public void OnAuthorization (AuthorizationContext filterContext) 
    { 
     Validate.IsNotNull (filterContext, "filterContext"); 

     if (!Enable) 
     { 
      return; 
     } 

     if (!filterContext.HttpContext.Request.IsSecureConnection) 
     { 
      // request is not SSL-protected, so throw or redirect 
      if (Redirect) 
      { 
       // form new URL 
       UriBuilder builder = new UriBuilder 
       { 
        Scheme = "https", 
        Host = filterContext.HttpContext.Request.Url.Host, 
        // use the RawUrl since it works with URL Rewriting 
        Path = filterContext.HttpContext.Request.RawUrl 
       }; 
       filterContext.Result = new RedirectResult (builder.ToString()); 
      } 
      else 
      { 
       throw new HttpException ((int)HttpStatusCode.Forbidden, "Access forbidden. The requested resource requires an SSL connection."); 
      } 
     } 
    } 

    public static bool Enable { get; set; } 
} 
+0

感謝を。私はアクションレベルではなくコントローラレベルでアタッチされた属性を持っています。ところで、私はあなたのEnable強化が好きです。 –

+0

どうすれば有効にするのですか?私の開発環境で私はそれがSSLであることを望んでいないと言う? – Blankman

+0

Validate.IsNotNull(...)とは何ですか?私はコードを貼り付けましたが、Validateはありません(私は先物をインストールしたくありません) – Blankman

関連する問題