允许匿名的MVC 3访问页面

时间:2018-03-22 06:48:53

标签: asp.net-mvc-3 web-config forms-authentication global-asax

我使用带有Windows身份验证的mvc 3创建项目。我的情况是,在登录页面中我创建了一个链接,将页面重定向到页面"忘记密码"。 但问题是。当我点击一个链接访问另一个页面而不登录。在我的网址浏览器中显示如下网址:" http://localhost:5074/Account/Login?ReturnUrl=%2fForgotPassword"。 无法重定向到控制器" forgotpassword"。

- 我尝试了一些方法,比如自定义web.config,global.asax和其他方法,但它不起作用

请更正我的代码并给我解决方案。

查看 - > login.cshtml

<a href="@Url.Action("Index", "ForgotPassword")">Forgot Password?</a>

控制器 - &gt; ForgotPassword

namespace EDIS.Controllers
{
public class ForgotPasswordController : MyController
{ 
    public ActionResult Index()
    {
        return View();
    }
}
}

的web.config

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<authorization>
  <deny users="?" />
</authorization>

的global.asax

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute()); 
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        DevExpressHelper.Theme = "DevEx";
    }

    protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
    {
        HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            string encTicket = authCookie.Value;
            if (!String.IsNullOrEmpty(encTicket))
            {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(encTicket);
                CustomIdentity id = new CustomIdentity(ticket);
                CustomPrincipal principal = new CustomPrincipal(id);
                HttpContext.Current.User = principal;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我可能误解了您的问题但是如何允许匿名用户访问控制器中的ForgotPassword功能。

[AllowAnonymous]  //...or you could move this attribute above Index()
namespace EDIS.Controllers
{
    public class ForgotPasswordController : MyController
    { 
        public ActionResult Index()
        {
            return View();
        }
    }
}