In this video we learn how to create a custom profile provider to store and retrieve data associated with a user profile in SQL Server tables separate from the standard ASP.NET membership provider’s aspnet_Profile table. The principles covered in this video will apply to creating profile providers for other databases like Access and Oracle. After learning how to create the custom profile provider we learn how to install the provider in a web site and see a demonstration of loading and saving profile data.
The following text is a software generated transcript of the video. Click on a minute link to jump to a location within the video
Minute 0
High this dispels this video you learn how to build a custom profile provider for ASP net may times the standard profile provider that comes with ASP net membership is sufficient for your needs but then again there may be times we like to create custom profile provider either to store your profile data in a different kind of database or you may still want a stored in SQL Server which is what the standard membership provider does it rather than the regular profile table he is like creates their own tables to store the profile data and more granular manner so you can do reporting or other things suffragan and do here first thing we need to do is to create a class library project that will contain the logic for a custom profile provider lets go up your torrent the solution at a new project lead a class library type project and let s call this
Minute 1
Profile provider sample and let s just stored in this folder here and we made a couple references in our class library project some other assemblies focused view all files so you can see the references are retreating here and add references to two different assemblies first one will be system configuration and the other one will be system Web now in order to create custom profile provider we need to inherit from the class called profile provider lets take our class here and Harry from system
Minute 2
Arrow system Web profile profile provider known hit the entity year would you get a seat use all of the abstract classes that we need to implement for our custom profile provider finish can see there is a fair number of them here that we have to implement some interest of time because they have quite a bit to react cover on this topic permitted to use bring in a class here that I ve created ahead of time which has all the code for these methods and I will go through and review what the code does and in each of the methods over here and just refresh our project there is a class that will bring in to include that in the project and now let s open up and take a look at it swimming time here in our classes I ve called it each day I profile
Minute 3
Provider put it in its own namespace as you see it still inherits from the system went profile profile provider class which required to do and then I dated a few imports up here so that don t have the type fully qualified namespace is in a number of different places so look back over here to SQL Server what I ve done is just create a database in SQL Server ATI profile provider and I ve installed ASP net membership in your membership tables were interested in are these two tables down at the bottom that it created what is called profiles and the others called profile data now profiles has a number of columns in it they don t have to have these column names literally but you do need to have columns that store a unique identifier a username and application name whether or not the user is anonymous as well as the date of the last activity
Minute 4
Because the implementation of the profile provider class requires that you be able to store that data into certain operations that relate to that date is a pretty standard set of columns in the other table we have here is called profile data and this is the table in which I ll store the specific profile data that I want for my particular application in this case at a unique identifier for each row of profile data
Minute 5
Al and store them locally most notably appearing class variables are connection string and application name now let s take a look at the get properties and set property value methods and these two methods as the names imply he get the values for our profile or set the values for profile closely look at how we get the values for profile for getting down to this method is the application context as well as a collection of properties that we can set so essentially what we have to do here in our method is go off and get the values for each of the properties in this case from the profile data table and store than in a collection of values corresponding to those properties are presently in a two year starting of I 95 is far to interact with some of the use information from the
Minute 6
Patient contacts so here what one do is find out airlines store the username and whether or not the user is authenticated because within a need to store that in some of our tables and their metadata but in a setup a sore procedure over in SQL Server that when a call to select the profile data from the database and add a few parameters down here starting on line 103 we were passing in the username application name and whether or not the user is anonymous because those are the three criteria that allow us to uniquely identify a profile for user in the profiles table and Tonya l seven were initializing a new instance of the settings property value collection which you can see a Carolina 93 is the return argument from this required from this method and remanded out here
Minute 7
Darr try block and within a open the connection as you do SQL data reader from ago to loop through each of the properties that were sent in down to this method and for each one of those within established what the property is by looking at the names are property in this case you receive TNT s LAT and long and if we find that property their market to take the value from the SQL data reader and assign it to the property value and notice we re creating a new instance of a local variable for the settings property value class which is where we re storing the property value and then down here on line 131 as we iterate through each of the
Minute 8
Here but to help you essentially what s happening is this one click should come in down to a method which has all the properties and then there s a separate collection that s been returned from the method and in debt collection we have each of the values for the property sunroom world done down here you see we are returning the collection of values that we have created sluts claps up this get properties method for slowdown and look at how we would set properties and what s happening here is essentially the reverse of what we just saw in that property values are getting in down to this method application context in essentially the same collection that we returned from the get property values method up here is being passed back to us and what we need to do then is to take each of the values in this collection and store them back in the database doodad we get some of this
Minute 9
Information than we did when we retrieve the property values that username but are not authenticated we set up a stored procedure here at work a call to save the data get some parameters which allow us to uniquely identify the user for saving the data and then we iterate through each of the inbound property values settings and we add that value to a parameter passed the inbound to the stored procedure and mom are done with the setting up parameters which is opening the connection and executing the query to save the data in the database okay let s look back over your SQL Server here are the two tables the profile data and eight profiles that we saw a little bit ago but down here to the stored procedures any of these procedures that have yet to get_prefix are with the membership provider but down here here are the various stored procedures that were
Minute 10
Using for our custom profile provider after getting our property values for our profile were using the profile data select and that stored procedure passes in three batters which allow us uniquely identifies the profile and before returning the data so we can stored in the profile in our code here online 21 is calling a separate stored procedure which is updating the last activity column in our profile record which visual call is up here right here at less activity in the service you did ever using to set the property values is profile data update something to look at that in this procedure for getting the three piece of information to uniquely identify the profile was as well as the profile data values here so we do in the stored procedure
Minute 11
Is we tried to find a profile idea based upon the username application name and whether not synonymous if we don t get a profile idea we assume this is a new profile so we ask you to separate stored procedure profiles in certain which creates a new profile and then we go down here and if it is a new profile we insert are three values into the profile data table with unique identifier or for other foreign key back to the profile if it s an existing record or profile then we update the three values in a profile data table and then once again we execute a stored procedure which updates the at last activity column in the profiles table another is the abstract methods to react to implement beyond the get and set property values will have to do with operations on groups of profiles
Minute 12
Temple down here on line 195 he can see is a method where we are to delete any inactive profiles and that s based upon a primer which is specified date so what this method would do is go out the gold of profiles last activity date and that s why it s important we update that value each timely access profile and he would delete all of those profiles that have not been active from the specified date began as a method after that we choose to lead all the profiles based on an array of usernames so essentially here s a bunch of usernames to legal profiles and then have a companion method here where we passed in a collection of profiles and we say okay to legal these profiles based upon the usernames better in the collection
Minute 13
And a series of methods that have to do with retrieving profiles first one here on line 298 is to find all the inactive profiles based on a user name to match so we pass in the username the activity date and then hear authentication option is passing in whether or not we re doing was an anonymous warn authenticate a user to take all of this information in down to the method and it looks up in the database Faisal was profiles and returns a collection of those profiles that we have some other methods here for finding profiles the next one is to find profiles by username and essentially the key in down right thing here is the username to match and I should mention that this can be not a fully qualified username but it could be a couple character like
Minute 14
Essay tea or something like that new final abuses that start with SAT and return the next we have that I get all a inactive profiles based simply on an inactivity date as compared to the previous method up here which was in activity date and the username we have get all profiles based on just in this case the authentication option page index and page size and then we have a method here he gets us the number of inactive profiles based on AP and the authentication option alerts on here to simplify that code for us is that each of these methods actually calls another method
Minute 15
Eyeing another private method here in our class which passes in the various arguments in this case we don t have some arguments so two of these inbound arguments are set to nothing and will score down to our methods region and in here we have two methods check parameters is just a little utility method that looks at the page indexing page size just calculates some information for that we will take a look at them in any detail has been a method of our look at get profile and post let s expand that and this receives number of arguments in down as they saw up in the methods above and it returns a collection of profiles essentially what this method is doing is taken using the arguments setting up a call to the stored procedure pack that passes all of those arguments in a stored procedure is doing the majority work by then filled
Minute 16
Ring and retrieving the appropriate profile records that we want and then down in this method for creating a profile for each return record and putting it into the collection is the world on with creating our custom Lighter now let s create a website and tested out as an mentioned in the beginning of the video we need to have a ASP net membership insult in order to test out and use the profile providers want done is to create a website ahead of time that has the ASP net membership install them will build upon that test that are profile provider good or solution and that existing website to our solution is right there and you notice the website he has a database in HDI profile provider and that s the database and we ve been looking at when we are pretty our custom profile provider over here in this class library project also on the web that can
Minute 17
Begin here weren t set up the membership provider of related elements to the first thing we need to do is install our provider here in our website in order to do that we need two things one is we need a reference to our assembly that has the provider and let s add that here and in this case what I m going to do is just reference the projects in our solution you could also create a bin folder and add the assembly manually into the bin folder to suddenly we want to do is have some information to install our provider in the web config so we re down here to where we want to put that and what we want as a profile element and we want to specify the default profile provider and recall that UTI SQL provider and then we want to add a provider in the
Minute 18
Providers element in the main delighted his face HDI SQL provider in the type is the fully qualified name for assembly and that would be profile provider sample provider in Leslie HDI profile providers in class and then the connection string we want to use that is the HD
Minute 19
Guy connection string which I set up and up above them is now also being used by the membership provider should might see up there on line 68 and the last thing is the application in who call this HCI videos then we have to set up the properties that are provider uses it as a want ad and their old one for the first one in name is GMT and the type used a system int 32 in the allow anonymous
Minute 20
When set to true then the next one to say and the next one that we want to add is called Latin and the type of this is system single and we also want to allow anonymous and the third and last one is a long type is also system single and allow anonymous said it would pay for these operations on our profiles at a new webpage here and we ll just take the default aspx
Minute 21
In the first he wanted to on this page is for had a long and status control let s get one of those bring it over here and will put them right in here that we want to add a couple text boxes where we can either view or edit some of the profile information so let s bring in a text box here and ago to Xbox for net in put that in there and let s just given a label in this case were to call this username for the first one hospitals based in their talent and a copy that has them in 21 to three more was little editing here is
Minute 22
Of the GMT as funds can have the latitude to him and his phones can have a launch to just remain the text boxes to TXT username 16 GMT latitude and longitude and let s go down here and and another a little paragraph and hear what one of put is couple buttons so let s just create ASP moves the TT by Mary Gero and this is
Minute 23
This one button clearer than an equal server in tax than this one will be clear in its copy that is known for two more buttons and we go talk in a city in the site ones can be called by the load and a third one and less funds in cold blood and say text here to move profile and save profile post over here to our code behind and the person we want to do is override the unloaded and
Minute 24
is and what we wanted to hear it is take the profile object and we want to test to see if it is anonymous and if it is I just remembered we want about one more thing over here in our page editor at appear above the username last to said a label he then led an ASP label here in IT rules on a run at equal server there we go now and go back over your article bind and what we want to do here is
Minute 25
That label things that tax for a label if we are anonymous when you put DES and then if are not anonymous to put no end when you take the username text box and we re going to assign the username from the profile to that conveyed us go back over your tour page will just go into the design mode and we want to set up an event handler for the click event for each one of these buttons and in a clear cynical take the
Minute 26
Xbox has said that equal to string empty to imitate the latitude and head said that to string empty and the same thing with the longitude there were no down here too are lured and you take the profile handling add GMT text box are considered equal to AT T value from the profile same of the latitude and longitude and the longitude
Minute 27
And and I say we do there are some who were again taken profile and were going to take the GN tea and value or property in the profile and it ll assigned the GMT textile UN is entertained for the latitude and the launch of them that unless they wouldn t do here he is at a simple login page showed lets you set a new eye near and a cold page login go here toward cool
Minute 28
Ox alien to get a login control news him of input data right here on this page dismembered one little mistake over here in our code behind me a tad indexes to tax us is correct that ultimately that would input peace call the save method over here in our state click event so let s put that in here somewhere saving the profile afterward on occasion be all set to test out our simple website here in our profile provider that started up without debugging and we initially are not logged in sober and anonymous user were given as good as our username lets put in some values here let s put one disc two and three quite quickly and then this button will save the profile backed
Minute 29
Database lets just do that here would clear out the profile information will keep the same usernames who only clicked low profile it should load in that information back again as it does now let s login and I set up one user in the database already in the membership provider called test user password Which output in the readme file a login our login as that user will be loads of profile here we should get the profile information that was previously stored in the database when I created his user and we do it s also one to three so let s just prove that this really works here by changing these values will save the profile watching logout go back to the anonymous user logo they are profiled still there will go back to login as the test user
Minute 30
And now or back as votes overall pass through the thread again there is test user let s load their profile and derelict I used that we just enter for saving our profile information here by invoking these buttons you could use other means to save the profile data whenever you wanted to and it s important to note here that we have set this up supports leaving both the profile for anonymous users that visit the site as well as authenticated users just to finish up this lets them go over to that SQL Server database here and I ve opened up the profiles the profile data tables here is our test user that was there before we started let s refresh this and now we ll see are anonymous user is seen here as well lets over the profile data
Minute 31
And we have here the old profile data will reexecute that then there is the profile data for both are anonymous and our test user so in summary to create a custom profile provider be created a class library project profile provider sample in it we have one class that inherited from the required base class of profile provider within its men s logic fully required abstract methods from the base class once we re done with her provider he created a simple websites tested out of here installed the provider by adding some elements to the web config file and then created a simple interface for testing a loading and saving of the profile information is quite a bit of information in this video on how to create a custom profile provider so I encourage you to download the sample source code and look at it in detail this is crisp
Minute 32
Pulls thanks for taking the time to watch this video on how to create a custom profile provider
Click here to go to original video page
Monday, August 25, 2008
Video: How Do I: Create a Custom Profile Provider?
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment