私は、MVC 4.0 プロジェクトのメソッド用のカスタム メンバーシップ プロバイダーを作成する任務を負っています。
属性 (?) に基づいて[Authorize]
、試行されたユーザーがメソッドの使用を許可されているかどうかを特定する必要があります。
現在、私はこれを持っています:
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = false)]
public class TestAuthorize : CodeAccessSecurityAttribute
{
public TestAuthorize(SecurityAction action)
: base(action)
{
}
public override IPermission CreatePermission()
{
throw new NotImplementedException();
}
}
追加する
return new PrincipalPermission(null,null,true)
と、権限が有効であり、ユーザーがメソッドにアクセスできることが期待されます。
追加する
return new PrincipalPermission(null.null,false)
と、アクセス許可が無効になると予想され、ユーザーはメソッドへのアクセスを拒否されます。
throw new SecurityException("You are denied access")
ただし、この例外がクライアント側で処理されない限り (try catch を使用して)、MVC アプリケーションを強制的に停止する を使用すると、継続が停止するだけです。
MVC プロジェクトでこの例外を処理することは可能ですか?
属性を使用してやりたいことの例として:
[TestAuthorize(SecurityAction.Demand)]
public List<string> GetList()
{
//if access, return new List();
//if no access, return null;
}