More in this section
Categories
Bloggers
Blogs RSS feed

Using the ASP.NET Sql Membership Provider in Sitefinity

by User Not Found

In the Sitefinity security space, all user and role management is handled through the ASP.NET Membership APIs. Sitefinity's backend uses its own custom membership and role providers, which utilize OpenAccess ORM to persist the users and roles into the Sitefinity database. Before the 4.1 release of Sitefinity, we didn't provide the option to substitute these providers with your own. Now doing this is a piece of cake.

We've seen a lot of requests asking how you can use the default ASP.NET Sql Membership Provider to log into the Sitefinity backend. This is a common need for users who are moving their existing applications to Sitefinity. Here is how you can do this:

Creating the Membership database needed by SqlMembershipProvider

If you are moving an existing solution, you've probably already performed this step. For the purposes of this sample, we're going to use an empty database and create the tables needed by the SqlMembershipProvider to store data. The most convenient step to do this is to use the aspnet_regsql.exe tool that ASP.NET provides for us. If we run it without any command-line option, it opens a wizard that guides us through the process:

This wizard is used by ASP.NET to either create the needed tables needed by the provider, or remove them if they are no longer necessary. We need to select the former option in the second step. 

The only information that the wizard needs is a connection string to the database it is going to work with. If you provide a name of a non-existing database, the wizard will create it for you.

When we complete these steps, the database will be ready for use by the SqlMembershipProvider. Now we can proceed with registering the provider with Sitefinity.

Registering the Provider with Sitefinity

These steps are done exactly like you would do them in a regular ASP.NET application. We need to include elements in the web.config file indicating the role and membership providers we are going to use. Also, we need to provide the connection string to the database we created in the previous step. Here are the new sections we've added to the web.config file:

<connectionStrings>
    <add name="AspNetMembership" connectionString="Data Source=localhost\sql2008;UID=sitefinity;PWD=sitefinity;initial catalog=NewSqlDatabase"/>
</connectionStrings>
 
...
 
<roleManager enabled="true">
    <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" connectionStringName="AspNetMembership" applicationName="/" type="System.Web.Security.SqlRoleProvider"/>
    </providers>
</roleManager>
 
...
 
<membership defaultProvider="AspNetSqlMembershipProvider">
    <providers>
        <clear/>
        <add connectionStringName="AspNetMembership" enablePasswordRetrieval="false"
                  enablePasswordReset="true" requiresQuestionAndAnswer="false"
                  requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
                  minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
                  applicationName="/" name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider"/>
    </providers>
</membership>

After this step your provider is fully functional, but has no data in it. If we want to login with a user from this provider, we have to first create a single user. Before we do this, though, you should remove the entry in web.config for the Sitefinity membership provider, if you have it. Sitefinity doesn't rely on the web.config to register its own providers, but the ASP.NET configuration tool might frown upon it. 

Creating a User in the New Provider

ASP.NET again provides a tool to do this, but in Visual Studio, rather than the command line. To access it, find the ASP.NET configuration button in the Solution Explorer, after you've opened your project in Visual Studio:

This will open a web administration tool with options to configure application settings.

We can manage users and roles by clicking on the Security tab. This tool uses the project's web config file to retrieve information about your providers, so if you encounter any error here, the most likely cause is something in your web.config.

On this screen, we only need to click the "Create user" link, and then provide all the information needed for the new user. After we click "Save", our new user is ready and persisted in the database.

Log in With the Newly Created User in the Sitefinity Backend

Up to now, we've created the membership database needed by SqlMembershipProvider, registered the provider with Sitefinity, and created a sample user in this provider. However, we still cannot log in with this user, because of missing rights to access the backend. To fix this, we need to assign the new user to a Sitefinity role able to access the backend. Logging in with an existing CMS user, we should be able to see the new provider and the newly created user.

The only thing left to do is go to the edit screen and assign the new user to the Administrators role.

After this, we should be able to log out, and then log in with our new user.

NOTE:
In order for the Password Reset functionality to work with users from the custom providers inheriting from System.Web.Security.MembershipProvider class, recoveryMailAddress can be specified in appSettings node in web.config using the provider's Name and _RecoveryMailAddress as the key.

I believe it will help if we demonstrate how we're retrieving the property on our end, namely:

var recoveryMailAddress = manager.RecoveryMailAddress;
if (String.IsNullOrEmpty(recoveryMailAddress))
{
recoveryMailAddress = ConfigurationManager.AppSettings[this.MembershipProvider + "_RecoveryMailAddress"];
if (String.IsNullOrEmpty(recoveryMailAddress))
recoveryMailAddress = string.Empty;
}

So for example if you have your custom MembershipProvider called "MyCustomProvider", the expected entry in appSettigns would be something like:
<appSettings>
    <add key="MyCustomProvider_RecoveryMailAddress" value="mypasswordrecovery@someserver.com" />

 

This sample demonstrates the scenario by using the ASP.NET SqlMembershipProvider. However, you can implement any membership provider and still use it with Sitefinity. For more information about implementing custom membership providers, you can visit MSDN. Let us know in the forums or support system about issues you encounter.

20 comments

Leave a comment
  1. apolimanseis Apr 03, 2018
    Η εταιρεία μας διαθέτει όλες τις απαραίτητες πιστοποιήσεις ώστε να μπορεί να εκτελεί εφαρμογές απολυμάνσεων σε κατοικημένους και επαγγελματικούς χώρους.apolimanseis
  2. .TvOye Apr 04, 2018
    What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much.TvOye
  3. αποφραξεις αθηνα Apr 04, 2018
    Η εταιρεία μας δραστηριοποιείται στις αποφράξεις από το 1940 σε όλη την Αττική. Είναι απ’ τις πιο ειδικευμένες εταιρείες στις αποφράξεις και τον καθαρισμό αποχετεύσεων. Διαθέτει εξελιγμένο εξοπλισμό, εξειδικευμένα προϊόντα και εκπαιδευμένο προσωπικό ώστε να παρέχει άρτιες υπηρεσίες σε παντός τύπου αποφράξεις αποχετευτικών αγωγών. Διαθέτει τις απαραίτητες πιστοποιήσεις από τους κρατικούς φορείς καθώς και ειδικά προϊόντα καθαρισμού, εγκεκριμένα από το υπουργείο αγροτικής ανάπτυξης. Για τις αποφράξεις των αποχετεύσεων και των υδραυλικών σωληνώσεων διατίθεται ειδικός επαγγελματικός εξοπλισμός, με κύριο στόχο την επίλυση του προβλήματος εύκολα, γρήγορα και αποτελεσματικά. Διαθέτει ειδικά εργαλεία υδροβολής, κοπτικά εργαλεία καθαρισμού ριζών δέντρων, που πιθανόν να διαπερνούν τα τοιχώματα των αποχετευτικών αγωγών, καθώς και ειδική κάμερα για ευκολότερο εντοπισμό του προβλήματος.Το κόστος των υπηρεσιών απόφραξης έχει διαμορφωθεί στις χαμηλότερες τιμές της αγοράς. Οι υπηρεσίες απόφραξης διατίθενται εικοσιτέσσερις ώρες το εικοσιτετράωρο σε όλες τις περιοχές της Αθήνας και του Πειραιά. Ειδικά εξοπλισμένα οχήματα βρίσκονται επί μονίμου βάσεως σε πολλές περιοχές της Αττικής, ώστε να είναι άμεση η εξυπηρέτηση του εκάστοτε πελάτη. ΑΠΟΦΡΑΞΕΙΣαποφραξεις αθηνα
  4. robert Apr 05, 2018
    Excellent .. Amazing .. I’ll bookmark your blog and take the feeds also…I’m happy to find so many useful info here in the post, we need work out more techniques in this regard, thanks for sharing.guidancesports
  5. robert Apr 05, 2018
    Τα τελευταία χρόνια επεκτείναμε σταδιακά τις δραστηριότητες μας εκτός τις αποφράξεις αποχετευτικών αγωγών και εκκενώσεις βόθρων, τώρα αναλαμβάνουμε υπεύθυνα απολυμάνσεις κτηρίων και έκδοση κοινοχρήστων, θέλοντας να προσφέρουμε ολοκληρωμένες υπηρεσίες στις κτιριακές ανάγκες.apofraxeis
  6. robo Apr 11, 2018
    Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.DETECTIVE MADRID
  7. robo Apr 17, 2018
    Keep up the good work , I read few posts on this web site and I conceive that your blog is very interesting and has sets of fantastic information.DETECTIVES PRIVADOS OVIEDO
  8. Glucometros Apr 21, 2018
    Great Article it its really informative and innovative keep us posted with new updates. its was really valuable. thanks a lot.Glucometros
  9. pakej bulan madu perhentian Apr 24, 2018
    Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!!pakej bulan madu perhentian
  10. robo Apr 25, 2018
    That appears to be excellent however i am still not too sure that I like it. At any rate will look far more into it and decide personally!BARRIDO ELECTRONICO
  11. sami May 01, 2018
    Wow what a Great Information about World Day its very nice informative post. thanks for the post.DETECTIVES PRIVADOS MADRID
  12. sohail khatri May 01, 2018
    It is a good site post without fail. Not too many people would actually, the way you just did. I am impressed that there is so much information about this subject that has been uncovered and you’ve defeated yourself this time, with so much quality. Good Works! voyance serieuse
  13. voyance-retour-amour.com May 03, 2018
    I'm glad to see the great detail here!.voyance-retour-amour.com
  14. sami May 03, 2018
    This was really an interesting topic and I kinda agree with what you have mentioned here!4k compute rmonitor
  15. sami May 08, 2018
    Wow what a Great Information about World Day its very nice informative post. thanks for the post.TSM Agency model staffing
  16. MONIKA May 10, 2018
    You know your projects stand out of the herd. There is something special about them. It seems to me all of them are really brilliant!Automatic Cat Feeders with Timer
  17. sami May 17, 2018
    I am definitely enjoying your website. You definitely have some great insight and great stories.DETECTIVES EN MADRID
  18. sami May 31, 2018
    Only aspire to mention ones content can be as incredible. This clarity with your post is superb and that i may think you’re a guru for this issue. High-quality along with your concur permit me to to seize your current give to keep modified by using approaching blog post. Thanks a lot hundreds of along with you should go on the pleasurable get the job done.Detectives Madrid
  19. robo Jun 07, 2018
    Great content material and great layout. Your website deserves all of the positive feedback it’s been getting.Detectives España
  20. sami Jun 23, 2018
    Thanks so much for this information. I have to let you know I concur on several of the points you make here and others may require some further review, but I can see your viewpoint.Investigadores privados Madrid

    Leave a comment