【问题标题】:MVC 3 Access page with allow anonymous允许匿名的 MVC 3 访问页面
【发布时间】:2018-08-31 12:17:53
【问题描述】:

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

-我尝试了一些方法,如自定义 web.config、global.asax 等,但没有奏效

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

查看-> login.cshtml

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

控制器 -> 忘记密码

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;
            }
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

    我可能误解了您的问题,但允许匿名用户访问控制器中的 ForgotPassword 功能怎么样?

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

    【讨论】:

    • 在 mvc 3 中,[AllowAnonymous] 什么都没有... =(
    • 您是使用域控制器还是其他方法验证您的应用程序?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多