Asp.Net静态支持同域令人困惑

问题描述 投票:0回答:1

我在这里读了一个答案他说:

静态对于应用程序域是唯一的,该应用程序域的所有用户将为每个静态属性共享相同的值

但我现在感到困惑,我为许多用户创建了一个项目,他们当然会分享同一个域名。

用于理解的示例代码:

public static class ApplicationSession
{
    private static readonly ICurrentSession Session;

    static ApplicationSession()
    {
        if (HttpContext.Current == null)
            Session = new ThreadedCurrentSession();
        else
        {
            Session = new WebCurrentSession();
        }
    }

    public static T GetObject<T>(string key) where T : class
    {
        return (T)Session.GetItem(key);
    }

    public static void SetObject<T>(string key, T t) where T : class
    {
        Session.SetItem(key, t);
    }

}

 public static SysUser CurrentUser
    {
        get
        {
            var currentusr = ApplicationSession.GetObject<SysUser>("CurrentUser");
            if (currentusr == null)
            {
                currentusr = SysUserAccessor.CreateEmptyUser();
                currentusr.SetRoles(new List<FrUserRole>());
                ApplicationSession.SetObject("CurrentUser", currentusr);
            }
            return currentusr;
        }
    }

SysUser是我的模型它拖动我的用户和我的用户角色...

如果他们进行身份验证,他们现在将共享相同的SysUser模型。或者别的请帮助:)

asp.net session static session-variables static-variables
1个回答
0
投票

您的ApplicationSession类是静态的,并在其静态构造函数上初始化Session对象。注意静态构造函数(或类型构造函数)在第一次访问类型期间仅为整个应用程序生命周期调用一次(例如,创建其成员,调用其静态属性等)。因此,在您的情况下,Session变量将仅使用ThreadedCurrentSession或WebCurrentSession类初始化一次(取决于第一次访问ApplicationSession类型时是否存在HttpContect)。

由于CurrentUser属性与Session对象相关,因此它将为所有用户请求提供相同的SysUser对象。

© www.soinside.com 2019 - 2024. All rights reserved.