私のアプリケーションでカスタムのAuthorize属性を使用して、クライアントと管理者からの要求を処理しようとしています。AuthorizeCoreがカスタム許可属性のために動作しません
私は、認証タイプが唯一の違いである別のアプリケーションで同じアプローチを使用しました。 1つはMicrosoftアカウントベースで、もう1つはFederated Servicesベースです。
AuthorizationCoreメソッドのオーバーライドでブレークポイントを設定しましたが、ユーザーがアプリケーションに初めてアクセスしようとしたときに1回だけ起動されるという問題があります。ログインページ。その後、それは再び解雇されません。ユーザーがコントローラ/アクションにアクセスするたびにそれを起動する必要があるので、ユーザーが正しい役割を持っているかどうかを確認することができます。私の理解では、Authorize属性が対象です。
マイコード:
public class AuthorizeUserAttribute : AuthorizeAttribute
{
/// <summary>
/// The Role required by the Action or Controller
/// </summary>
public UserRole RequireRole { get; set; }
/// <summary>
/// Authorization Logic
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
//Result = new AuthorizationResult();
bool isAuthorized = base.AuthorizeCore(httpContext);
if (isAuthorized)
{
using (ApplicationDbContext context = new ApplicationDbContext())
{
ApplicationUser user = context.ApplicationUsers.FirstOrDefault(u => u.EmailAddress.Equals(httpContext.User.Identity.Name, StringComparison.OrdinalIgnoreCase));
}
// ... Check if user has the required role
}
return isAuthorized;
}
/// <summary>
/// Redirect the user
/// </summary>
/// <param name="filterContext"></param>
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
// Handle the request if the user does not have the required role
base.HandleUnauthorizedRequest(filterContext);
}
}
[AuthorizeUser(RequireRole = Core.Models.Users.UserRole.User)]
public ActionResult Index()
{
return View();
}
を次のように私は任意の助けが理解されるであろう属性を使用しています。 おかげ
どのように 'AuthorizeUserAttribute'を使用していますか?いくつかのコード例を投稿してください。 – NightOwl888
ここで 'isAuthorized'をfalseに設定していますか? –
@ NightOwl888私は、基本レイアウトである – ederoock