2012-03-09 5 views
0

私はMVCにはかなり新しく、実際にはまだ[Authorize]のハングを持っていません。基本的には、ある地域ではアクセスする前に電子メールアドレス、名前、会社名を入力する必要があるウェブサイトがあります。パスワードなし、登録なしなど。Adminがあり、ユーザーは電子メールアドレスとパスワードを使用してサインインする必要があります。複数の承認リクエスト

私の質問は、二重認証の状況をどのように実装するのですか?

また、Adminエリアにいるときは、自分が管理しているサイト、つまり現在のサイトよりも多くのコンテンツをアップロードできます。私は彼らが管理しているサイトを保持するデータベースを持っています。 URLは/ Admin/Site /のようになりますが、一度adminにログインすると、Admin/Site2に行くことができないようにします。Site2は管理者ではありません。

+0

ロールを使用する必要があります。 [http://stackoverflow.com/questions/6404254/mvc-3-dynamic-authorization-of-multiple-roles-and-users][1] [1]:のhttp://のstackoverflow。 com/questions/6404254/mvc-3-multiple-roles-and-usersの動的承認 –

答えて

1

私はその人が匿名で、前もって詳細を提供していればファイルをダウンロードできると仮定しています。その場合は、Authorization属性を無視して独自の属性を記述します。ここに簡単な例があります。それは設定されるクッキーに依存します。

public class CheckEmailAddressAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     var request = filterContext.HttpContext.Request; 
     // if the cookie isn't set the a user never provided their details 
     if (request.Cookies["mycookiename"] == null) 
     { 
      // Set it to the correct result, to redirect the user to provide their email address 
      filterContext.Result = new ViewResult { ViewName = "GetEmailAddress" }; 
      filterContext.HttpContext.Response.AppendCookie(new HttpCookie("mycookiename", "true")); 
     } 
     else 
     { 
      // Don't do anything, the user already provided their email address 
     } 

    } 
} 

ダウンロードしたコントローラで次のように指定します。

public class DownloadController : Controller 
{ 
    [CheckEmailAddress] 
    public ActionResult Download(string name) 
    { 
     // Implement download 
    }  
} 

残っているのは、電子メールアドレスが設定されているときにクッキーを設定することだけです。私はあなたがそれをする方法を知っていると仮定します。また、「returnUrl」パラメータがあることを確認して、電子メールアドレスを入力した後でダウンロードページにリダイレクトすることもできます。

EDIT OP 1として、私たちはここで更新クラスがあり、人はその詳細を入力した場合を除き、クッキーを設定する必要はありません。

public class CheckEmailAddressAttribute : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     var request = filterContext.HttpContext.Request; 
     // if the cookie isn't set the a user never provided their details 
     if (request.Cookies["mycookiename"] == null) 
     { 
      // Set it to the correct result, to redirect the user to provide their email address 
      filterContext.Result = new ViewResult { ViewName = "GetEmailAddress" }; 
      // filterContext.HttpContext.Response.AppendCookie(new HttpCookie("mycookiename", "true")); 
     } 
     else 
     { 
      // Don't do anything, the user already provided their email address 
     } 

    } 
} 
+0

例えば、私は[CheckEmailAddress]を、詳細を尋ねるページを除くプレス領域内のすべてについていますか? – ediblecode

+0

また、この例では、ページ上に移動してCookieを設定するだけです。それらはページにリダイレクトされ、その後単に後方にナビゲートされ、次にそれらはクッキーを持っています。 – ediblecode

+0

私の更新を見てください。属性にクッキーを設定すべきではありません。 – bloudraak

関連する問題