Asp.Net中判断是否登录,及是否有权限?
不需要在每个页面都做判段,
方法一:只需要做以下处理即可
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace HeathRoom.Controllers { public class BaseController : Controller { // // GET: /Base/ protected override void OnActionExecuting(ActionExecutingContext filterContext) { if (Request.Cookies["homeid"] == null) { //filterContext.Result = RedirectToRoute("Login"); filterContext.Result = RedirectToRoute(new { Controller = "Login", Action = "Login" }); base.OnActionExecuting(filterContext); } } } }
然后,让所有的Controller继承自BaseController。
方法二: 使用自定义AuthorizeAttribute属性
customAuthorizedAttribute:authorizeAttribute
{
override public void OnAuthorization (AuthorizationContext filterContext)
{
// 进行逻辑判断
}
}
给需要进行验证的controller 或者action添加属性标签[customAuthorized]