我们的ASP.Net会员控件中有一个鬼

时间:2010-12-15 19:02:33

标签: asp.net-membership forms-authentication membership session-timeout

我们设置了一个使用2个不同数据库的网站。他们现在设置的方式是,当你去www.website.com并登录时,一旦经过身份验证,你将拥有一个设置为website1ConnectionString的cookie。每次我们使用linq调用datacontext时,我们都会发送功能来检查cookie名称并获取cooresponding连接字符串。 EX PortalDataContext db = new PortalDataContext(AuthenticatedUser.ConnectionString);如果用户访问www.website.com/2ndlogin,则会对用户进行身份验证,并使用website2ConnectionString cookie设置cookie。我们现在遇到一个问题,用户名和guid会随机改变为其他用户,从而显示错误的信息。

我们通过写出与登录用户关联的用户名并浏览网站来注意到这一点。在一些不活动之后,随机显示在每个页面顶部的用户名将随GUID一起更改为另一个用户。有时它会改变回来,有时我们会被迫退出并重新登录。

我们最近发生过这样的事情,即database1上的用户已将其用户名和GUID更改为Database2上的用户。

我们使用的AuthenticatedUser类如下所示:

public static MembershipUser GetUser()
    {
        string connection = AuthenticatedUser.ConnectionString;
        string provider = "";
        if (connection.Contains("website2"))
        {
            provider = "website2MembershipProvider";
        }
        else
        {
            provider = "AspNetSqlMembershipProvider";
        }

        MembershipProvider prov = Membership.Providers[provider];
        MembershipUser m = prov.GetUser(UserName, true);

        return m;
    }

    public static MembershipProvider GetMembershipProvider()
    {
        string connection = AuthenticatedUser.ConnectionString;
        string provider = "";
        if (connection.Contains("website2"))
        {
            provider = "website2MembershipProvider";
        }
        else
        {
            provider = "AspNetSqlMembershipProvider";
        }

        MembershipProvider prov = Membership.Providers[provider];
        return prov;
    }

    public static Guid LoginUserID
    {
        get
        {
            Guid g = new Guid();

            string connection = AuthenticatedUser.ConnectionString;
            string provider = "";
            if (connection.Contains("website2"))
            {
                provider = "website2MembershipProvider";
            }
            else
            {
                provider = "AspNetSqlMembershipProvider";
            }

            MembershipProvider prov = Membership.Providers[provider];
            MembershipUser m = prov.GetUser(UserName, true);
            if (m != null)
            {
                g = (Guid)m.ProviderUserKey;
            }

            return g;
        }
    }

    private static string _UserName = "";
    public static string UserName
    {
        get
        {
            if (String.IsNullOrEmpty(_UserName))
            {
                if (Membership.GetUser() != null)
                {
                    return Membership.GetUser().UserName;
                }
            }
            else
            {
                return _UserName;
            }

            return "";
        }

        set
        {
            _UserName = value;
        }
    }

public static string ConnectionString
    {
        get
        {
            HttpCookie myCookie = HttpContext.Current.Request.Cookies["connectionString"];
            return GetConnectionStringFromName(myCookie);
        }
        set
        {
            if (HttpContext.Current.Request.Cookies["connectionString"] != null)
            {
                ExpireCookies(HttpContext.Current);
            }
            var allCookies = HttpContext.Current.Request.Cookies.AllKeys;
            HttpCookie cookie = new HttpCookie("connectionString");
            cookie.Value = value;
            cookie.Expires = DateTime.Now.AddYears(100);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
    }

1 个答案:

答案 0 :(得分:0)

您的UserName属性不使用所选的提供程序,它始终使用默认提供程序。你是如何使用UserName的?此外,何时将m.ProviderUserKey设置为GUID?


不过,你可以重构一些重复的代码:

public static MembershipUser GetUser()
{
    return GetMembershipProvider().GetUser(UserName, true);
}

public static MembershipProvider GetMembershipProvider()
{
    string connection = AuthenticatedUser.ConnectionString;
    string provider;
    if (connection.Contains("website2"))
    {
        provider = "website2MembershipProvider";
    }
    else
    {
        provider = "AspNetSqlMembershipProvider";
    }

    return Membership.Providers[provider];
}

public static Guid LoginUserID
{
    get
    {
        Guid g = new Guid();

        MembershipUser m = GetUser();
        if (m != null)
        {
            g = (Guid)m.ProviderUserKey;
        }

        return g;
    }
 }