我有一個產品控制器「product_editor」所需的角色來訪問大多數方法。有一些操作不需要授權,因此[AllowAnonymous]很好。但是,我有一個操作,我想要求他們已登錄,但他們可能不是產品編輯器。在控制器中授權一個動作,但不需要角色
是否有一種簡單的方法來聲明一個動作?
你可以看到一對夫婦我嘗試註釋掉
[Authorize(Roles = "product_editor")]
public class ProductController : Controller
{
#region Public Actions
[AllowAnonymous]
public ActionResult Search(string keyword...
[AllowAnonymous]
public ActionResult Details(string id...
#endregion
//[Authorize]
//[Authorize(Roles="")]
public ActionResult AuthorizedDownload(long id, string step)
{
SecureDownloadLink link = SecureDownloadLink.Load(id);
if(link.belongsTo(HttpContext.Current.User.Identity.Name))
{
//Allow download
}
else
{
//Return 404 Error
}
}
}
- 編輯 -
找到一個可行的解決方案,但由於我的身份驗證的其餘部分完成很想基於屬性的解決方案屬性和[AllowAnonymous]有點誤導。
[AllowAnonymous]
public ActionResult AuthorizedDownload(long id, string step)
{
if (!User.Identity.IsAuthenticated)
return RedirectToAction("Login", "Account", new { ReturnUrl = Request.Url.LocalPath });
....
}