应用程序连接多个数据库并设置成员资格cookie

时间:2018-01-16 05:44:37

标签: c# asp.net asp.net-mvc web-config asp.net-membership

我正在编写一个方法,用它设置所有数据库连接字符串。该方法有一些参数,如连接字符串和cookie域(用于单点登录状态),...

我可以使用作为参数发送的指定连接字符串获取成员资格和角色信息。

            //membership
            System.Web.Security.SqlMembershipProvider mp = new System.Web.Security.SqlMembershipProvider();
            System.Collections.Specialized.NameValueCollection config_Membership = new System.Collections.Specialized.NameValueCollection();
            config_Membership.Add("connectionString", connectionstring);
            config_Membership.Add("applicationName", "/");
            mp.Initialize("SQL_test_Membership", config_Membership);
            var u = mp.GetUser(username, false);
            int TotalRecords = 0;
            var p = mp.GetAllUsers(0, 1, out TotalRecords);

           //login
            bool valid = mp.ValidateUser(username, password);

            System.Web.Security.SqlRoleProvider rp = new System.Web.Security.SqlRoleProvider();
            System.Collections.Specialized.NameValueCollection config_Role = new System.Collections.Specialized.NameValueCollection();
            config_Role.Add("connectionString", connectionstring);
            config_Role.Add("applicationName", "/");
            rp.Initialize("SQL_test_Role", config_Role);
            var roles = rp.GetRolesForUser(username);

我想获得类似上面代码的ProfileBase信息

https://technet.microsoft.com/nl-nl/library/system.web.profile.profilebase.initialize(v=vs.85).aspx

我找到了以下代码:

            System.Web.Profile.ProfileBase pro = new System.Web.Profile.ProfileBase();
            System.Collections.Specialized.NameValueCollection config_profile = new System.Collections.Specialized.NameValueCollection();
            config_profile.Add("connectionString", connectionstring);
            config_profile.Add("applicationName", "/");
            pro.Initialize(?????)

但我不知道如何将参数发送到pro.Initialize(),任何人都可以帮助我吗?感谢。

1 个答案:

答案 0 :(得分:1)

我的问题解决了。我的代码被更改了:

            //login
            bool valid = mp.ValidateUser(username, password);

            if (valid)
            {
                System.Web.Profile.ProfileBase pro = new System.Web.Profile.ProfileBase();
                System.Collections.Specialized.NameValueCollection config_profile = new System.Collections.Specialized.NameValueCollection();
                config_profile.Add("connectionString", connectionstring);
                config_profile.Add("applicationName", "/");
                pro.Initialize(username, true);


                string Name = pro.GetPropertyValue("Name").ToString();
                string Family = pro.GetPropertyValue("Family").ToString();
                string phone = pro.GetPropertyValue("Phone").ToString();
                string address = pro.GetPropertyValue("Address").ToString();


            }