Other membership provider implementations

Aug 21, 2012 at 5:54 PM

Any idea on that question?

http://stackoverflow.com/questions/12049841/simple-membership-provider-vs-universal-vs-code-first

Thanks

Coordinator
Aug 22, 2012 at 9:13 AM

Difference is Universal is nearly the same as original with added support for SQL Compact and Azure. Simple membership is a bit simplified, has a few additional modern implementations like token based password resets and works great with ASP.NET Webpages. Both are written by using barebone SQL and have clunky table structures. Code-first is written in EF therefore it supports more databases, it is simplified a bit more as it can only be used on 1 site (in my current implementation) has neater tables which are easily extendable. Code-first provides a better prograiming experience.

Oct 22, 2012 at 2:24 PM

The funny thing is that in MVC4 sample project they have also created model with CodeFirst approach using SimpleProvider as a backend. What would you say on this? :)

Coordinator
Oct 22, 2012 at 4:24 PM

That its a hack job, its the best implementation they've come up so far on an outdated membership provider interface.

Its still not entity framework centric, its even more complex now and breaks the good things about code-first. And they just replaced profiles nothing more.

1. You still cannot change the User/Role fields, whereas you can rename them to french german or any language you want with mine.

2. You cannot evolve with database migrations

3. You cannot switch database engines on the fly

4. You cannot link User table to your any other database table

5. Write another benefit here and repeat a few dozen time...

Oct 22, 2012 at 4:33 PM
Edited Oct 22, 2012 at 4:38 PM

Good to know. Thanks for explanation :)

Btw. have you considered to use System.Web.Providers or WebMatrix.WebData.SimpleMembershipProvider as a backend for your provider as both derive from System.Web.Security one?

Coordinator
Oct 22, 2012 at 4:51 PM

Both of them implement System.Web.Security.MembershipProvider as is this one, and are written with base sql, whereas mine is with code-first. Why would I want to extend those??

Code-first membership provider already has nearly everything those two have. 

 

Oct 22, 2012 at 4:59 PM
Edited Oct 22, 2012 at 4:59 PM

I thought System.Web.Security.MembershipProvider is sql based too, so you need override some methods anyway and if so I thought that you could derive some extra functionality like token based password resets, but if you say that you've already written it your way then there is really no point.

Coordinator
Oct 22, 2012 at 6:09 PM

No the MembershipProvider is not sql dependant, the default implementation the SqlMembershipProvider is sql based.

Heres an example of what I was talking about:

Delete user from CodeFirstMembership:

   public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            if (string.IsNullOrEmpty(username))
            {
                return false;
            }
            using (DataContext Context = new DataContext())
            {
                User User = null;
                User = Context.Users.FirstOrDefault(Usr => Usr.Username == username);
                if (User != null)
                {
                    Context.Users.Remove(User);
                    Context.SaveChanges();
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

And here is from SimpleMembership:

 // Inherited from MembershipProvider ==> Forwarded to previous provider if this provider hasn't been initialized
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            if (!InitializeCalled)
            {
                return PreviousProvider.DeleteUser(username, deleteAllRelatedData);
            }

            using (var db = ConnectToDatabase())
            {
                int userId = GetUserId(db, SafeUserTableName, SafeUserNameColumn, SafeUserIdColumn, username);
                if (userId == -1)
                {
                    return false; // User not found
                }

                int deleted = db.Execute(@"DELETE FROM " + SafeUserTableName + " WHERE " + SafeUserIdColumn + " = @0", userId);
                bool returnValue = (deleted == 1);

                //if (deleteAllRelatedData) {
                // REVIEW: do we really want to delete from the user table?
                //}
                return returnValue;
            }
        }

See the db.Execute and TSQL queries.

 

Also you if you use CodeFirstMembership you can use the standard membership like: Membership.CreateUser without the need to pre-initialize the provider as its done in SimpleMembership (and yes I know its done on the static Membership.CreateUser too, but that's not the point) but you can also use WebSecurity to extend it with token based resets etc. You can copy the methods from asp.net 4 source code and implement it code-first style and shown above.

Oct 22, 2012 at 6:32 PM

Ah, you are perfectly right, I mismatched sqlmembership with membership. Thank you a lot for helping me understand that! :)

Oct 22, 2012 at 6:56 PM

Btw. do you have any plans to implement OAuth into your provider like in SimpleProvider?

Coordinator
Oct 24, 2012 at 8:08 PM

Nope, not for now, copy it from simplemembership if you wish.