私自身の質問に答える: 最後に、カスタム actionFilter を作成しました。最初は、[authorize] を [AuthorizeCheckProfile] にサブクラス化するというパスを取りました。しかし、その後、ユース ケースが間違っていることに気付きました。ユーザー プロファイルが存在しない場合、サイトのログイン専用部分をプロファイル作成ページにリダイレクトさせたくありませんでした。プロファイルのないログイン ユーザーの場合、自分のサイトのどのページもそのページにリダイレクトするようにしたかったのです。私が確認したくない唯一の場所は、実際のプロファイル作成です。コードは次のとおりです。
public class AssertProfileAttribute : ActionFilterAttribute {
public AssertProfileAttribute() {
}
public override void OnActionExecuting(ActionExecutingContext filterContext) {
if (filterContext.HttpContext.Request.IsAuthenticated == false)
return;
//we have a user, but does he have a profile?
if (filterContext.HttpContext.Session["UserProfile"] == null) { //not in the session
IUnityContainer container = filterContext.HttpContext.Application["container"] as IUnityContainer;
Profile hasProfile = container.Resolve<IProfileRepository>().GetUserProfile(Membership.GetUser());
if (hasProfile == null) {
//we have to redirect to the create profile
string returnURL = filterContext.HttpContext.Request.AppRelativeCurrentExecutionFilePath;
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Profile", action = "Create", returnTo = returnURL }));
} else {
//he has a profile but we haven't put it in session yet
filterContext.HttpContext.Session["UserProfile"] = hasProfile;
}
}
}
}
プロファイルをセッションキーに保存するという副作用があります。このようにして簡単に取得できるため、他のカスタム フィルターを使用してすべてのリクエストでさらにロール チェックを行うことができます。実装では、データベース アクセスに Unity とリポジトリを使用します。
コミュニティに感謝します。