Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET:第一次向HttpContext添加内容_Asp.net_Security_Asp.net Mvc 5_Httpcontext - Fatal编程技术网

ASP.NET:第一次向HttpContext添加内容

ASP.NET:第一次向HttpContext添加内容,asp.net,security,asp.net-mvc-5,httpcontext,Asp.net,Security,Asp.net Mvc 5,Httpcontext,我们正在使用来保存我们自己的SecurityContext public static ISecurityContext Current { get { if (!(HttpContext.Current.Items["SecurityContext"] is ISecurityContext)) { HttpContext.Current.Items["SecurityContext"] = new SecurityCont

我们正在使用来保存我们自己的SecurityContext

public static ISecurityContext Current {          
     get {
        if (!(HttpContext.Current.Items["SecurityContext"] is ISecurityContext)) {
            HttpContext.Current.Items["SecurityContext"] = new SecurityContext(...);
        }
        return HttpContext.Current.Items["SecurityContext"] as ISecurityContext;

     }
}

现在的问题是,在哪里创建SecurityContext并将其添加到HttpContext。在查看时,我想
iaauthenticationfilter
是第一个这样做的机会。将SecurityContext添加到HttpContext是一个好主意还是有更好的“位置”呢?

我建议使用更长期的存储,比如会话状态,而不是HttpContext.Current.Items,只是因为使用后者的范围较短(根据HTTP请求)这意味着您必须设置每个请求的
SecurityContext
。(当然,除非这是一项要求。)

使用会话状态意味着您可以创建一次上下文,并将其存储在会话状态中以供将来使用(直到会话过期)

存储

var context = Session["SecurityContext"] as SecurityContext;

if (context != null) {
//.......
}
会话[“SecurityContext”]=新的SecurityContext()

阅读

var context = Session["SecurityContext"] as SecurityContext;

if (context != null) {
//.......
}

看来你想要点什么。一个可能是一个很好的选择,用于构建控制器的样式。这样,当
HttpContext
准备好使用时,您就不必费心了

回答你的问题。 在
global.asax
中,使用
Application\u PreRequestHandlerExecute
方法实例化和存储服务实现

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            var context = (sender as HttpApplication).Context;
            context.Items.Add("IService", new Service());
        }
因为这个方法甚至在创建任何控制器之前就执行了,所以它的实现将是可用的。您可以创建检索实例的服务定位器类

 public class ServiceLocator
    {
        public static IService Current
        {
            get
            {
                return HttpContext.Current.Items["IService"] as IService;
            }
        }
    }
并在控制器中使用该类

  public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = ServiceLocator.Current.Now.ToString();
            return View();
        }
    .........

使用IAAuthenticationFilter创建和设置SecurityContext的缺点是什么?如果您不打算在使用IAAuthenticationFilter之前使用SecurityContext对象,则没有缺点。只是在这种情况下使用身份验证筛选器似乎很奇怪。您看到任何情况吗,身份验证前在何处使用SecurityContext?是的,这似乎有点奇怪,但我们不喜欢使用global.asax中的“神奇方法”。是的,使用DI并让框架在控制器中注入依赖项。会话似乎不是一个好的解决方案,因为其中使用NHibernate和每个请求的会话。将SecurityContext与domainobject(例如,当前用户)一起使用时,nhibernate会引发异常。