【发布时间】:2020-07-09 22:40:27
【问题描述】:
所以我想在控制器中运行任何操作之前检查会话是否存在。我找到了这些解决方案link1,link2 并尝试实施它们。
这是我的SessionAuthorize 课程,这里一切正常:
public class SessionAuthorize : ActionFilterAttribute
{
private readonly SessionManager _sessionManager;
public SessionAuthorize(SessionManager sessionManager)
{
_sessionManager = sessionManager;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (!_sessionManager.IsLoggedIn)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary {
{ "Controller", "Home" },
{ "Action", "Login" }
});
}
}
}
现在,问题是当我尝试在控制器中调用它时。所以我应该通过像这样添加[SessionAuthorize] 属性来调用:
[SessionAuthorize]
public class AccountController : Controller
{
//code
}
但它给了我一个错误,我没有正确调用它。我相信这与我在SessionAuthorize 中用于访问会话的依赖注入有关。但我现在不知道怎么称呼它。
错误信息是:
'没有给出与所需形式相对应的参数 参数 'sessionManager' 的 'SessionAuthorize.SessionAuthorize(SessionManager)'
所以我想知道我应该如何在我的控制器中使用它
【问题讨论】:
-
SessionManager 类是什么?是您编写的课程还是来自图书馆的课程?
标签: asp.net asp.net-mvc asp.net-core .net-core