ASP.Net MVCログイン認証
5250 ワード
public class Verify : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
var user = filterContext.HttpContext.Session["CurrentUser"];
if (filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
// Action
{
return;
}
else if (filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))
// Controller
{
return;
}
else if (user == null || string.IsNullOrWhiteSpace(user.ToString()))
//
{
filterContext.Result = new RedirectResult("../Login/Login");
}
else
{
return;
}
}
}
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new Verify());
}
}
[Verify]
public class LoginController : Controller
{
}
public class LoginController : Controller
{
[Verify]
public ActionResult UserInfo(Models.UserInfo userInfo)
{
}
}
public class LoginController : Controller
{
///
///
///
///
///
[HttpPost]
[AllowAnonymous]//
public ActionResult Login(Models.UserInfo userInfo)
{
}
}