カスタム承認属性を使用して、ユーザーのアクセス許可レベルに基づいてユーザーのアクセスを承認しています。許可されていないユーザーをリダイレクトして(たとえば、ユーザーがアクセスレベルの削除なしで請求書を削除しようとしている)、拒否されたページにアクセスする必要があります。
カスタム属性が機能しています。ただし、不正なユーザーアクセスの場合、ブラウザには何も表示されません。
コントローラーコード。
public class InvoiceController : Controller
{
[AuthorizeUser(AccessLevel = "Create")]
public ActionResult CreateNewInvoice()
{
//...
return View();
}
[AuthorizeUser(AccessLevel = "Delete")]
public ActionResult DeleteInvoice(...)
{
//...
return View();
}
// more codes/ methods etc.
}
カスタム属性クラスコード。
public class AuthorizeUserAttribute : AuthorizeAttribute
{
// Custom property
public string AccessLevel { get; set; }
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
var isAuthorized = base.AuthorizeCore(httpContext);
if (!isAuthorized)
{
return false;
}
string privilegeLevels = string.Join("", GetUserRights(httpContext.User.Identity.Name.ToString())); // Call another method to get rights of the user from DB
if (privilegeLevels.Contains(this.AccessLevel))
{
return true;
}
else
{
return false;
}
}
}
あなたがこれについてあなたの経験を共有することができるかどうか感謝します。