how to use Profile.GetProfile() in a library class?

后端 未结 3 1950
野的像风
野的像风 2021-01-20 01:24

I cant figure out how to use Profile.GetProfile() method in a library class. I tried using this method in a Page.aspx.cs and it worked perfectly.

How ca

3条回答
  •  旧时难觅i
    2021-01-20 02:03

    You can use ProfileBase, but you lose type-safety. You can mitigate that with careful casting and error handling.

        string user = "Steve"; // The username you are trying to get the profile for.
        bool isAuthenticated = false;
    
            MembershipUser mu = Membership.GetUser(user);
    
            if (mu != null)
            {
                // User exists - Try to load profile 
    
                ProfileBase pb = ProfileBase.Create(user, isAuthenticated);
    
                if (pb != null)
                {
                    // Profile loaded - Try to access profile data element.
                    // ProfileBase stores data as objects in a Dictionary 
                    // so you have to cast and check that the cast succeeds.
    
                    string myData = (string)pb["MyKey"];
    
                    if (!string.IsNullOrWhiteSpace(myData))            
                    {
                        // Woo-hoo - We're in data city, baby!
                        Console.WriteLine("Is this your card? " + myData);
                    }
                }        
            }
    

提交回复
热议问题