防止登录的用户访问登录页面asp.net MVC应用程序

本文关键字:登录 asp net MVC 应用程序 访问 用户 | 更新日期: 2023-09-27 17:53:53

这是我的控制器。它不工作的登录用户阻止访问登录页面。请帮帮我。我已经使用了请求。是认证和用户。控制器中的标识方法

[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
    if (Request.IsAuthenticated)
    {
        return RedirectToAction("Main","Home");
    }
   else if (!this.ModelState.IsValid)
    {
        return this.View(model);
    }
   else if (Membership.ValidateUser(model.Username, model.Password))
    {
        FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
        if (this.Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
            && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/''"))
        {
            return this.Redirect(returnUrl);
        }
        return this.RedirectToAction("Main", "Home");
    }
    this.ModelState.AddModelError(string.Empty,"Invalid Credentials.");
    this.ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect.");
    return this.View(model);
}

防止登录的用户访问登录页面asp.net MVC应用程序

我在我的代码中尝试了这行

bool test = Request.IsAuthenticated;

总是返回false。然而,我在我的应用程序中使用windows身份验证。如果你有登录屏幕,我假设你没有。我在网站上发现了这篇关于总是从Request.IsAuthenticated接收错误值的文章。显然,如果你从这个标志接收到正确的值,你的代码应该工作。如果这不是答案,让我知道,我可能会再研究一点。

请求。IsAuthenticated总是false

我就是这么做的:

public bool IsAuthenticated
{
    get
    {
        return this._context.User != null && 
               this._context.User.Identity != null &&
               this._context.User.Identity.IsAuthenticated;
    }
}