gpt4 book ai didi

c# - 自定义 AuthorizeAttribute 中的 session 为空

转载 作者:太空宇宙 更新时间:2023-11-03 12:52:16 25 4
gpt4 key购买 nike

<分区>

我有一个 asp.net MVC 应用程序,使用框架 4.6,我创建了一个自定义的 AuthorizeAttribute。在我的类(class)中,我重写了 AuthorizeCore 方法并访问 session 以验证一些身份验证信息。但有时当我尝试在 AuthorizeCore 方法中访问属性 session 时,我会遇到 NullReferenceException,因为 session 属性为 null。

问题是间歇性的,当我在同一操作中同时访问时会发生。如果我打开页面并多次刷新页面(在浏览器中按 F5),它会显示错误。

我注意到当 session 为空时,处理程序也为空。

我正在使用 StateServer 模式来存储我的 session 。我尝试更改为 InProc 模式,但我遇到了同样的问题。我不知道这是否有所不同,但我正在使用 Ninject MVC。

我的自定义属性:

public class CustomAuthorizeAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContextBase)
{
if (!httpContextBase.User.Identity.IsAuthenticated)
{
return false;
}

var infoUser = httpContextBase.Session["infoUser"];

...
}
}

我的 Controller :

public class HomeController
{
[CustomAuthorize]
[OutputCache(CacheProfile = CACHE_PRINCIPAL)]
public ActionResult Index()
{
return View();
}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com