電子メールアドレスが検証されていない場合、ユーザーを別のアクションにリダイレクトしようとしています。ことは、私はそれらがログアウトされることを望んでいない、私はちょうどそれらをリダイレクトしたいです。コントローラのOnAuthorizationでこれを行うと、期待どおりにリダイレクトされますが、ユーザーは認証されません。なぜこのことが分かりませんか?私のコードは次のようになります:OnAuthorizationのMVCリダイレクトにより、認証が失敗する
protected override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
//_applicationService.CurrentUser is populated correctly at this point
// from Controller.User
if (_applicationService.CurrentUser != null)
{
if (_applicationService.CurrentUser.EmailVerified != true)
{
var url = new UrlHelper(filterContext.RequestContext);
var verifyEmailUrl = url.Action("EmailVerificationRequired", "Account", null);
filterContext.Result = new RedirectResult(verifyEmailUrl);
}
}
}
注:わかりやすくするために不要なコードを削除しました。 _applicationService.CurrentUserには現在のユーザーが移入され、ユーザーはそのポイントに到達すると正しく認証されました。しかし、リダイレクト後、ユーザーはもはや認証されません。
どのようにしてこのリダイレクトをビルドインユーザー許可に影響することなく達成できますか?
私はOnActionExecutingに自分のコードを入れようとしましたが、カスタムActionFilterAttributeでも実装しようとしましたが、このリダイレクトをどこに置いても 'User'(つまり、System.Security.Principal .IPrincipal Controller.User)が認証されるのを防ぎます。
私はここで何が欠けていますか?これが理にかなってほしい。どんな助けでも大歓迎です。私のログインアクションのためのダーリンの要求に応じて
:
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
string errorMessage = "The username or password is incorrect";
if (ModelState.IsValid)
{
if (_contextExecutor.ExecuteContextForModel<LoginContextModel, bool>(new LoginContextModel(){
LoginViewModel = model
}))
{
ViewBag.CurrentUser = _applicationService.CurrentUser;
_formsAuthenticationService.SetAuthCookie(model.LoginEmailAddress, model.RememberMe);
if (_applicationService.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Index", "Home").Success("Thank you for logging in.");
}
else
{
errorMessage = "Email address not found or invalid password.";
}
}
return View(model).Error(errorMessage);
}
あなたはあなたの 'LogOn'アクションをどのように認証していますか? –
@DarinDimitrov - 私はログインアクションを要求通りに追加しました。 – soupy1976
これは私が探していたものです!多くの時間を節約 – JosephDoggie