3

クライアントアプリケーションが、オーディオファイルを「チャンク」でMVC3サイトにアップロードしています。クライアントはHttpWebRequestPOSTを使用します。サーバー上で ASP.NET MVC3アプリケーションで複数の認証プロバイダを同時に使用できますか?

、私は次のようしているコントローラのアクション:現在

[Authorize] 
    [HttpPost] 
    public JsonResult RecieveChunk(string id, [ModelBinder(typeof(AudioChunkModelBinder))] byte[] audio) 
     { 
      //Process chunk 

      var chunk = new AudioChunk 
      { 
       ThoughtId = Guid.Parse(id), 
       Data = audio 
      }; 

      //Process chunk by BL 

      return new JsonResult {Data = "Success"}; 
     } 

、承認をビルトインAspNetMemebershipProvider処理しているので、クライアントアプリケーションは、最初にクッキーを取得し、ログオンページで認証する必要がありますa CookieContainerし、サーバーへの呼び出しを行い、データのチャンクをアップロードします。

クライアントは、事前に登録する必要なく、匿名でオーディオファイルをサーバーにアップロードできるようにします。クライアントアプリケーションコードは、ファイルが同じデバイスからアップロードされるたびに同じGUIDを提供します。

両方のカテゴリのユーザーが同じRecieveChunkアクションを共有して欲しいと思います。しかし、それらは匿名で(ちょうど​​)、またはログオン/パスの組み合わせでauthrizedする必要があります。

2つの異なるコントローラを2つの異なる認証プロバイダにリンクできますか? [Authorize]アクションがマークされている3番目のコントローラーは、いずれかのプロバイダーがユーザーにCookie(またはその他の認証方法)を与えた場合にアクションを許可します。

一般的にASP.NET MVC3では可能ですか?

+0

:だからあなたのような何かができる

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public class RequireHttpsAttribute : FilterAttribute, IAuthorizationFilter { public virtual void OnAuthorization(AuthorizationContext filterContext) { if (filterContext == null) throw new ArgumentNullException("filterContext"); if (filterContext.HttpContext.Request.IsSecureConnection) return; this.HandleNonHttpsRequest(filterContext); } protected virtual void HandleNonHttpsRequest(AuthorizationContext filterContext) { if (!string.Equals(filterContext.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase)) throw new InvalidOperationException(MvcResources.RequireHttpsAttribute_MustUseSsl); string url = "https://" + filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl; filterContext.Result = (ActionResult) new RedirectResult(url); } } 

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] public sealed class ChildActionOnlyAttribute : FilterAttribute, IAuthorizationFilter { public void OnAuthorization(AuthorizationContext filterContext) { if (filterContext == null) throw new ArgumentNullException("filterContext"); if (!filterContext.IsChildAction) throw Error.ChildActionOnlyAttribute_MustBeInChildRequest(filterContext.ActionDescriptor); } } 

そして、ここではRequireHttpsAttribute実装ですアクションは認可を必要としない*ため、その属性を持つべきではありません。代わりに、アクション内のコードを使用して、ログインしているかどうかを判断し、それに応じて行動する必要があります。 –

+0

はい、それは私がやっていることです。だから私は答えがちょうどいいと仮定しています - いいえ、メンバーシッププロバイダーの1つの実装だけがアプリケーション全体で同時に動作しますか? –

+0

うーん、いいえ。それはあなたがやっていることではありません。そこにはAuthorize属性があります。また、匿名ユーザーは定義上、*認証されません。だから、あなたが探しているのは、複数のメンバーシップ・プロバイダーではありません。 –

答えて

1

コメントで説明したように、FilterAttribute classのカスタム実装を作成し、IAuthorizationFilter interfaceを実装できます。例えば、ここにChildActionOnlyAttribute実装です:あなたがここで言うことから、アップロードを

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 
public sealed class CustomAuthorizeAttribute : FilterAttribute, IAuthorizationFilter 
{ 
    public void OnAuthorization(AuthorizationContext filterContext) 
    { 
    if (filterContext == null) 
     throw new ArgumentNullException("filterContext"); 

    var guidPresent = CheckForGuid(); 

    if (!filterContext.HttpContext.User.Identity.IsAuthenticated && !guidPresent) 
     throw new InvalidOperationException("Must authenticate properly") 
    } 
} 
関連する問題