【问题标题】:How to allow anonymous access to mvc pages in aspx project - Error message 401.2如何允许匿名访问 aspx 项目中的 mvc 页面 - 错误消息 401.2
【发布时间】:2020-06-11 23:27:40
【问题描述】:

如何在 ASPX 项目中允许这个 MVC 页面匿名?

在传统的 ASP.NET 项目(aspx 页面)中,有一些 MVC 页面在框架内共存和工作。我最近添加了一个 MVC 控制器和 forgotpassword.cshtml 页面,该页面仅在我登录时工作。控制器具有允许匿名属性,但在此项目中不支持此属性,因为项目中仅存在 MVC 框架的一部分。我在应用程序开始请求中设置了“Context.Response.SuppressFormsAuthenticationRedirect”,这有助于重新引导回登录,但我收到了未经授权的错误。

错误:

Access is denied.
Description: An error occurred while accessing the resources required to serve this request. The server may not be configured for access to the requested URL.

Error message 401.2.: Unauthorized: Logon failed due to server configuration.  Verify that you have permission to view this directory or page based on the credentials you supplied and the authentication methods enabled on the Web server.  Contact the Web server's administrator for additional assistance.

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3535.0

AccountController.cs

[Authorize]
public class AccountController : SessionReadController
{
    public AccountController(IUserSecurityService userSecurityService, ILogger logger)
        : base()
    {
        _logger = logger;
    }

    [AllowAnonymous]
    public ActionResult ForgotPassword()
    {
        return View();
    }
 }

Global.asax.cs

private void Application_BeginRequest(object sender, EventArgs e)
{
    if (Context.Request.Path.Contains("forgotpassword"))
    {
        Context.Response.SuppressFormsAuthenticationRedirect = true;
    }

}

【问题讨论】:

    标签: c# asp.net asp.net-mvc


    【解决方案1】:

    您需要在类级别中删除属性 [Authorize] 以使 [AllowAnonymous] 属性起作用,并将 [AuthorizedAction] 属性放入您想要验证的所需函数中。

    public class AccountController : SessionReadController
    {
        [Authorize]
        public ActionResult AuthorizedPage()
        {
        }
    
        [AllowAnonymous]
          public ActionResult ForgotPassword()
          {
             return View();
          }
    }
    

    【讨论】:

    猜你喜欢
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-23
    • 1970-01-01
    • 2016-08-31
    相关资源
    最近更新 更多