身份验证和RouteConfig

时间:2019-04-23 18:52:08

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

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

这是我的配置

RouteConfig

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

WebConfig

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

           ...
        }
    }   
}

1 个答案:

答案 0 :(得分:0)

这是一个示例,我希望如何使用actionfilter属性,

actionfilterattribute的代码;

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”相关,否则您可以在globalfilters中进行设置,该过滤器将对所有应用程序产生影响。