【问题标题】:authentication and RouteConfig身份验证和 RouteConfig
【发布时间】:2019-09-13 00:28:14
【问题描述】:

在第一次加载时,我的应用程序应该显示“localhost/home/index”,但总是将我重定向到“localhost/Account/login?ReturnUrl=%2f”

这是我的配置

路由配置

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

网络配置

 <authentication mode="Forms">
      <forms defaultUrl="~/Home/index" loginUrl="~/Account/login" timeout="60" />
 </authentication>

控制器

 [AllowAnonymous]
 public class HomeController : BaseController
  [AllowAnonymous]
  public class AccountController : BaseController

我不想重定向到“localhost/account/login”

更新

第一次加载总是进入条件并执行RedirectToLoginPage方法

public class BaseController : Controller
{
  protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var load = (HomeModel)Session["SessionLayout"] == null;

...
            if (load)
            {
                FormsAuthentication.SignOut();
                //FormsAuthentication.RedirectToLoginPage();

                PageLoad();
            }

           ...
        }
    }   
}

【问题讨论】:

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


    【解决方案1】:

    这是一个例子,我如何使用actionfilter属性,希望对你有帮助。

    actionfilter属性的代码;

    namespace  CP.Controladores
    {
    public class ValidateCaptchaAttribute : ActionFilterAttribute{
    
        public override void OnActionExecuting(ActionExecutingContext 
        filterContext)
        {
             filterContext.ActionParameters["CaptchaIsValid"] = recaptchaResponse.IsValid;
             base.OnActionExecuting(filterContext);
        }
    }
    

    控制器代码。

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    [CP.Controladores.ValidateCaptcha()]
    public ActionResult MailDeContacto(FormCollection values, bool 
    CaptchaIsValid)
    {
        // Do something
    }
    

    标记方法,此过滤器仅对“MailDeContacto”Action 有相关性,否则可以在全局过滤器中设置,过滤器将影响所有应用程序。

    【讨论】:

    • 是的,它有标签[AllowAnonymous] public class BaseController : Controller
    • 在我的 web.config 我使用 请删除 defaultUrl=" ~/Home/index”,请告诉我。
    • 结果还是一样
    • 很少见。尝试移除 basecontroller,让 homecontroller 简单地继承控制器类。我有它和你一样,它工作得很好(没有基本控制器)也许错误就在那里!
    • 我是对的,从 HomeController 中删除 BaseController 并且它工作正常。 [AllowAnonymous] 公共类 HomeController : 控制器。
    猜你喜欢
    • 2015-02-02
    • 2018-02-24
    • 2015-08-10
    • 2012-10-14
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    相关资源
    最近更新 更多