Saving Additional User Data in MemberhipProvider / FormsAuthenticationTicket

I have implemented my own custom MemberhipProvider with a custom data store. There are no problems at the moment. I would like people to log in using their email address instead of their username. Since I have my own data warehouse, this is not a serious problem, I can simply transfer the email as the username for the MembershipProvider.

My question is: how to store additional user data in FormsAuthenticationTicket? I want to save a couple of things that will never change, like their UserId, First / Last Name and Country. I started learning how to create a FormsAuthenticationTicket with UserData, but quickly got confused. How to store several things in this UserData and how easy it is to read this data on every ASP.NET MVC2 page. I found many samples, none of which seemed so large in terms of MVC2. There should be an easy way to do this.

It makes no sense to read UserId, First / Last Name and Country from the database for each query, because it will never change. Also, although I want the user to log in using my email address, I would like to save their UserId in an auth cookie so that it can be used for almost every user database request, and not email ( because in all the tables user data is saved along with UserId - and not by email, because technically the email can be changed - I already realized that this happens when it comes to MembershipProvider).

What are the best methods for storing additional user data like this in ASP.NET MVC2?

+3
7

( ) FormsAuthenticationTicket.UserData . http://www.west-wind.com/weblog/posts/899303.aspx

- , , /, .

    public class UserState
    {
        public int Id { get; set; }

        public Guid ActivationId { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public bool IsAdmin { get; set; }

        // Serialize    
        public override string ToString()
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string result = serializer.Serialize(this);
            return result;
        }

        // Deserialize
        public static UserState FromString(string text)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Deserialize<UserState>(text);
        }
    }
}
+4

cookie FormsAuthenticationTicket UserData, , . JSON, , Last, First names .

, , .

+2

. auth - cookie / . / . , - , .

.

+1

?

+1

Profile , , , , :)

ASP.Net

SQL,

, , .

0

- , . , , web.config, , . , 1 ( ).

Web.Config:

    <profile>
        <properties>
            <add name="DepartmentNumber"/>
        </properties>
    </profile>

:

ProfileCommon newProf = Profile.GetProfile(username);
                newProf.DepartmentNumber = "12";   //Or whatever string data you have.
                newProf.Save();

:

String departmentNumber = Profile.DepartmentNumber;    //Data is stored as String.
0

If you are worried about round trip requests every time you need this data, complete a combination of session status and profile. Store the data in the profile, and then, after your login is successful, take these values ​​into the session state. Use them from session state during this session. You can even add a check that will request a profile if the session state is empty.

0
source

Source: https://habr.com/ru/post/1757689/


All Articles