間違いなく。それが可能であるだけでなく、それはかなり簡単です。また、ASP.NETの役割を「アクティビティ」と考えることができれば、何も派生させる必要はありません。必要なものはすべて組み込まれています。
securityService
これらの例は、ミドルウェアアプリケーションと通信するサービスであり、2つの方法があることを前提GetUser
としていGetUserRoles
ます。
ログインアクションメソッド
[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (!ModelState.IsValid) return View();
var user = securityService.GetUser(model.Username, model.Password);
if (user == null)
{
ModelState.AddModelError("", "Username or password are incorrect.");
return View();
}
FormsAuthentication.SetAuthCookie(user.Username, model.Remember);
return Redirect(returnUrl);
}
Global.asax.csで
protected void Application_AuthenticateRequest()
{
if (Request.IsAuthenticated)
{
string username = User.Identity.Name;
string[] roles = securityService.GetUserRoles(username);
IIdentity identity = new GenericIdentity(username);
Context.User = new GenericPrincipal(identity, roles);
}
}
それでおしまい。Login
認証を処理し(ユーザーがログインするとき)、Application_AuthenticateRequest
承認を処理します(すべての要求で)。Authorize(Roles = "XYZ")
次に、 「XYZ」がメソッドから返されるものと一致することを確認して、アクションメソッドの装飾に進みますGetUserRoles
。