CodeSmith Community
Your Code. Your Way. Faster!

MembershipProvider for .netTiers

Latest post 06-26-2008 4:19 PM by Ben Ogle. 30 replies.
  • 05-01-2008 2:10 AM In reply to

    • Ben Ogle
    • Top 500 Contributor
    • Joined on 05-01-2008
    • Posts 13
    • Points 525

    Re: MembershipProvider for .netTiers

    I wrote a patch for this: http://community.codesmithtools.com/forums/p/8361/30743.aspx 

    I am _really_ hoping it (or some derivative of) is integrated so I dont have to keep a fork of the templates. This is crazy handy if for nothing more than having custom columns in the user table (First/Last name in a profile! What slop!!).

    Ben

    • Post Points: 35
  • 06-24-2008 10:17 PM In reply to

    • peelay
    • Top 150 Contributor
    • Joined on 02-12-2006
    • Toronto, Ontario
    • Posts 31
    • Points 870

    Re: MembershipProvider for .netTiers

    Hi there,

    I'd love to take advantage of the MembershipUserProvider but my initial attempts have left me frustrated. I'm wondering if anyone who's managed to put this to use might mind sharing some sample code. When attempting to create a new EntityMembershipUser I get back MembershipCreateStatus.ProviderError status and no user is created. Stepping through the code I noticed some odd behaviour, like applicationName always being null, even though it's set in the web.config file, this is obviously a required field as nulls aren't allowed for the column so it could be part of the problem (however hard coding value for it didn't solve problem either).

    Here's a sample of what I'm trying, maybe someone can let me know if I'm way off.

    MembershipCreateStatus userStatus;

    EntityMembershipProvider newUserProvider = new EntityMembershipProvider();

    EntityMembershipUser newUserProfile = (EntityMembershipUser)newUserProvider.CreateUser(UserName.Text, Password.Text, Email.Text, null, null, false, Guid.NewGuid(), out userStatus);

    Thanks in advance!

    Phill

    • Post Points: 35
  • 06-25-2008 12:31 PM In reply to

    • Ben Ogle
    • Top 500 Contributor
    • Joined on 05-01-2008
    • Posts 13
    • Points 525

    Re: MembershipProvider for .netTiers

     Have you set it up as the membership provider in the web.config?

    		<membership defaultProvider="EntityMembershipProvider">
    <providers>
    <remove name="EntityMembershipProvider"/>
    <add name="EntityMembershipProvider" type="MyGen.Namespace.EntityMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="8" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
    </providers>
    </membership>

     Then instead of creating a new instance, you use the static object that the framework provides:

    System.Web.Security.Membership.Provider 

     

    • Post Points: 35
  • 06-25-2008 1:53 PM In reply to

    • peelay
    • Top 150 Contributor
    • Joined on 02-12-2006
    • Toronto, Ontario
    • Posts 31
    • Points 870

    Re: MembershipProvider for .netTiers

    Hi Ben,

    Thanks for the response. I have added the provider to the web.config as above, the only difference is that instead of applicationName="/" I have applicationName="MyAppName". I tried changing to / and same result.

    I also tried doing the following, to use the normal provider that is being overridden like this:

    MembershipCreateStatus userStatus;

    MembershipUser newuser = Membership.CreateUser(UserName.Text.Trim(), Password.Text, Email.Text.Trim().Replace(" ", string.Empty), null, null, false, out userStatus);

    However it still doesn't work, but this time I get a different error. Instead of a generic provider error I get a DuplicateEmail error, yet I don't have any users in aspnet_Users, aspnet_membership,  or in my UserTable so it's impossible for there to be a duplicate email. Any other tips or troubleshooting options would be greatly appreciated.

    Also, as a test I manually created a user in my db to see if I could log in and using the standard login control it wouldn't work. Is this because the overriding the MembershipProvider with the EntityMembershipProvider breaks the standard login controls? Or should it work? Maybe it's all related to some missing element?

     Thanks again!!!

    Phill

     

    • Post Points: 35
  • 06-25-2008 2:10 PM In reply to

    • Ben Ogle
    • Top 500 Contributor
    • Joined on 05-01-2008
    • Posts 13
    • Points 525

    Re: MembershipProvider for .netTiers

    It should work with the standard membership controls. I use the standard login control with this provider. 

    Do you have the nettiers provider properly setup in the web.config? Maybe the SMTP config (assuming the thing is trying to send an email to the user when you create):

    <system.net>
    <mailSettings>
    <smtp>
    <network
    host="smtp.myhost.net"
    port="25"
    userName="someguy"
    password="wtfomg"/>
    </smtp>
    </mailSettings>
    </system.net>

     

    Thats all I can think of right now. Different errors == progress.

    Ben

    • Post Points: 35
  • 06-25-2008 3:35 PM In reply to

    • peelay
    • Top 150 Contributor
    • Joined on 02-12-2006
    • Toronto, Ontario
    • Posts 31
    • Points 870

    Re: MembershipProvider for .netTiers

    Ok, as you said, different error could be progress. The DuplicateEmail is actually the first error I got but since you let me know I was on the right track I dug a bit deeper and found out why it was giving me this incorrect error. In the generated code EntityMembershipProvider.cs on line 386 the following:

    if ( RequiresUniqueEmail && GetUserNameByEmail( email) != "") was the problem, GetUserNameByEmail returns null not an empty string if it doesn't find any users with that email address. So changing it to != null allowed me to get further along. Now of course I'm at another road block. I'll also give a bit more detail on my db setup and maybe I'm misunderstanding usage of this Provider. I have an aspnet_users table (and all other memebership tables) in my database, I also have a UserProfile table that has all the aspnet_membership columns as well as extended properties that I want to track for the user. This table has ProviderUserKey as the primary key, int and auto increments. I also have a UserId (uniqueidentifier) column that has a FK relationship to aspnet_users table. With that in mind things still aren't working. One I notice that the Provider checks to see if ProviderUserKey ash been supplied and if not sets to 0, why would it do this at all if the database is set to auto increment? Shouldn't we leave this out all together and let the DB deal with the creation of the number? And secondly I get the following error because of my FK relationship to the aspnet_users table. I assume it's because it's trying to insert record into my UserProfile table but the user hasn't been created yet. Maybe this is where I'm not understanding the point of this provider, if it's to override memberrship provider, using Membership.CreateUser() would add user to aspnet_users as well as populate membership data, does this not do that? Am I way off?

    The error:

    The INSERT statement conflicted with the FOREIGN KEY constraint "FK_UserProfile_aspnet_Users". The conflict occurred in database "Operation10", table "dbo.aspnet_Users", column 'UserId'.

    Yet again, thanks so much for your help!

    Phill

    • Post Points: 5
  • 06-26-2008 9:56 AM In reply to

    • peelay
    • Top 150 Contributor
    • Joined on 02-12-2006
    • Toronto, Ontario
    • Posts 31
    • Points 870

    Re: MembershipProvider for .netTiers

    As a minor update, by removing the FK relationship to the aspnet_users table I'm able to insert a record into UserProfile, but nothing gets created in aspnet_users. Is this how it should work? If it's extending the regular Membership Provider shouldn't a user get created there as well? Also now that I have a user created (but only in my custom Membership table called UserProfile) I can't log in using login controls, I assume this is because the control is not looking at this table?

    Any more ideas/direction would be great as I'm soon getting to the point of having to scrap all this work and just go back to using SQLProfileTableProvider and regular Membership Provider.

    Thanks again!

    Phill

    • Post Points: 35
  • 06-26-2008 12:28 PM In reply to

    • Ben Ogle
    • Top 500 Contributor
    • Joined on 05-01-2008
    • Posts 13
    • Points 525

    Re: MembershipProvider for .netTiers

     Well, no matter the server code you have running, if there is a foreign key constraint on the UserProfiles table and you create a profile row without a corresponding user, you'll get an error. You shouldnt have to remove the constraint, you just need to create the user first. 

    Anyway, I am not really following the latest post. you say:

    'but only in my custom Membership table called UserProfile'

    To be clear, this patch has nothing to do with profiles. We dont even use profiles (thats why I am using this patch, so we can add columns to the member table). My suggestion is to disable all the profile setting/getting/handling code and try to figure out why the provider isnt creating a user when you call Membership.Provider.CreateUser. If/when that works, then move onto the logging in action. All the stock Login control does is call Membership.Provider.ValidateUser (then it does some cookie magic that has nothing to do with the provider), so try to get that to work. You could just setup a new project that uses your generated code and uses all the stock auth controls. That's what I would do, reduce it to the smallest amount of potential errors.

    Now, are you sure you are generating the code correctly? You need to point to the proper membership table, the proper PKID of that table, etc. (all of which is done through the codesmith interface). Is that all taken care of?

    Troubleshooting this is difficult without seeing the schema, your codesmith settings, your web.config, and your code.

    Ben

    • Post Points: 35
  • 06-26-2008 12:36 PM In reply to

    • Ben Ogle
    • Top 500 Contributor
    • Joined on 05-01-2008
    • Posts 13
    • Points 525

    Re: MembershipProvider for .netTiers

    It just occurred to me that you are probably using the version that is contained within this thread. Are you? Or are you using my patch? Or are you using the 2.3 beta (http://code.google.com/p/nettiers/downloads/list)? The beta has this patch included. 

    Ben

    • Post Points: 35
  • 06-26-2008 12:52 PM In reply to

    • peelay
    • Top 150 Contributor
    • Joined on 02-12-2006
    • Toronto, Ontario
    • Posts 31
    • Points 870

    Re: MembershipProvider for .netTiers

    Hi again,

    I'm thinking I probably shouldn't have called my Extended Membership table UserProfile as it's a bit misleading. I don't use any "Profile" code anywhere, I just called my table UserProfile because extending the Membership table with extra columns is much like having a Profile.

    When Generating I'm pointing to a column called ProviderUserKey that's an auto increment integer and is also defined as the Primary Key for that table. As I mention in my last update, I've got it all working to the point that it creates a record in my modified Membership table. However it doesn't create the user in aspnet_users table. I assume that it also should create the user here as well correct? This is what the provider would rely on to perform "ValidateUser". When I look at the code in EntityMembershipProvider CreateUser, after doing some checks for unique username/email, it creates a basic EntiyMembershipUser (in my case called UserProfile as that's the name of my extended membership table) then it calls DataRepository.UserProfileProvider.Save(user), but nowhere do I see any code that would call System.Web.UI.Security Membership.CreateUser that I assume would be required to insert a new user into aspnet_users.

    This is probably a case of looking at this too closely or too long, but I have to be missing something here. As an fyi, I'm using the latest code from SVN.

    Cheers,

    Phill

    p.s. I know this is a pain for you but your help is appreciated. If you want we can take it offline and then I'll just post the solution for the benefit of others.

    • Post Points: 35
  • 06-26-2008 1:00 PM In reply to

    • peelay
    • Top 150 Contributor
    • Joined on 02-12-2006
    • Toronto, Ontario
    • Posts 31
    • Points 870

    Re: MembershipProvider for .netTiers

    I've actually been getting latest from here: http://nettiers.googlecode.com/svn/trunk

    I will try the link to the beta you just sent to see if that does the trick. Will let you know what happens.

    Thanks!

    • Post Points: 5
  • 06-26-2008 1:08 PM In reply to

    • Ben Ogle
    • Top 500 Contributor
    • Joined on 05-01-2008
    • Posts 13
    • Points 525

    Re: MembershipProvider for .netTiers

    So you have an aspnet_users table and the membership table? Can I see the schema for both? With the custom membership table that nettiers talks to, you dont need the aspnet_users table. Your membership table replaces all that factory asp.net junk. Then the EntityMembershipProvider reaplces the factory provider that talks to the aspnet_users table. With this patch in place, there is nothing that will talk to the aspnet_users table. 

    When you install the EntityMembershipProvider in your webconfig, System.Web.UI.Security.Membership.Provider.CreateUser calls your EntityMembershipProvider, which, as you've pointed out, calls the custom membership table's provider to do that actual work. EntityMembershipProvider and the associated classes are only there to plug generated nettiers code into the Microsoft way. 

    Ben

    • Post Points: 35
  • 06-26-2008 2:20 PM In reply to

    • peelay
    • Top 150 Contributor
    • Joined on 02-12-2006
    • Toronto, Ontario
    • Posts 31
    • Points 870

    Re: MembershipProvider for .netTiers

    Ok, I think I'm finally there! Thanks so much for your help and patience. Other than a the minor bug on line 386 that always reports duplicate emails if you require unique email, because it's checking for empty string when null is what is returned, it wasn't that tough after all. The big issue was that I was under the impression this extended MS membership "junk" as you put it, specifiecally the aspnet_membership table and functionality. I thought all the other User/Membership/Profile stuff was still used and required. So in large part it was my Relationships in the DB with those tables that caused me grief.

    So I'm off to the races, creating users AND logging in! Yay!

    Related, I noticed that I have a column in my tabled called UserName and the generated code creates it as User.Username so the build fails as it's case senstive. Just a minor bug that's easy to fix but you might want to check in with the other bug mentioned above.

     Thanks again Ben! Just needed to talk it though!

    Phill

    • Post Points: 35
  • 06-26-2008 2:44 PM In reply to

    • Ben Ogle
    • Top 500 Contributor
    • Joined on 05-01-2008
    • Posts 13
    • Points 525

    Re: MembershipProvider for .netTiers

    Glad you got it going. I will look into those bugs.

    Where was the case-sensitivity error? In the EntityMembershipProvider, in the custom membership table provider, or in the custom membership entity? I am guessing it was EntityMembershipProvider calling User.Username where User is a Member (entity for the custom membership table) and defines the property as UserName. Am I way off?

    Ben

    • Post Points: 35
  • 06-26-2008 3:47 PM In reply to

    • peelay
    • Top 150 Contributor
    • Joined on 02-12-2006
    • Toronto, Ontario
    • Posts 31
    • Points 870

    Re: MembershipProvider for .netTiers

    The case issues occur at the following locations:

    EntityMembershipProvider.cs line 416 (In CreateUser where the new User obj is being created)

    EntityMembershipProvider.cs line 706 (in GetUserNameByEmail in the return statement) Note you could also return "" here instead of null when count == 0 and then leave line 386 alone.

    EntityMembershipUser.cs line 75

     

    Once again thanks!

    Phill

    • Post Points: 35
Page 2 of 3 (31 items) < Previous 1 2 3 Next > | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems