控制器验证
重写Controller方法
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCo…
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MvcMovie.Filters
{
public class AuthenticationControllor : Controller
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Session.GetString("_Name") == null)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Home", action = "Index", test = "hello" }));
}
base.OnActionExecuting(filterContext);
}
}
}
public class HelloWorldController : AuthenticationControllor
{
// GET: /HelloWorld/
public ActionResult Index()
{
return View();
}
}
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Routing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MvcMovie.Filters
{
// 登录认证特性
public class AuthenticationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Session.GetString("_Name") == null)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Home", action = "Index", test = "hello" }));
}
base.OnActionExecuting(filterContext);
}
}
}
[Authentication]
public IActionResult Contact()
{
ViewData["Message"] = "Your contact page.";
return View();
}
HttpContext.Session.SetString("_Name", "bing");
HttpContext.Session.GetString("_Name");
如果这篇文章对你有所帮助,可以通过下边的“打赏”功能进行小额的打赏。
本网站部分内容来源于互联网,如有侵犯版权请来信告知,我们将立即处理。