失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > asp.net mvc 中的Form身份认证

asp.net mvc 中的Form身份认证

时间:2020-09-01 02:36:18

相关推荐

asp.net mvc 中的Form身份认证

默认的MVC4程序在配置文件web.config中有以下配置

<authentication mode="Forms">

<forms loginUrl="~/Account/Login" timeout="2880" />

</authentication>

设置后可以以匿名用户访问所有的Action,若想限制登陆后才能访问,可以在Action方法前加[Authorize] 属性

[Authorize]

public ActionResult Index()

{

ViewBag.Message = "修改此模板以快速启动你的 MVC 应用程序。";

return View();

}

如果嫌麻烦,不想在每个Action前加[Authorize],可以注册过滤器,这样每个Action都要登陆后才能访问

public static void RegisterGlobalFilters(GlobalFilterCollection filters)

{

filters.Add(new HandleErrorAttribute());

filters.Add(new System.Web.Mvc.AuthorizeAttribute());

}

设置过滤器后,如果想某个Action不登陆就可以访问,可以设置属性[AllowAnonymous]

[AllowAnonymous]

public ActionResult About()

{

ViewBag.Message = "你的应用程序说明页。";

return View();

}

如果觉得《asp.net mvc 中的Form身份认证》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。