Mark 909
Mark 909

Reputation: 1835

Error When Creating User - ASP.NET System.Web.Providers

I'm using the new ASP.NET Universal Providers as described at this Hanselman blog post comment:

I can get it wired up to authenticate using the following:

<profile defaultProvider="DefaultProfileProvider" >
  <providers>
    <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider1" connectionStringName="DefaultConnection" applicationName="/"/>
  </providers>
</profile>
<membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider" connectionStringName="DefaultConnection"               enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"             applicationName="/" />
  </providers>
</membership>
<roleManager defaultProvider="DefaultRoleProvider">
  <providers>
    <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider" connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
</roleManager>

I then try and create a new user using the following code

object akey = System.Guid.NewGuid();
MembershipCreateStatus status;
var member = new System.Web.Providers.DefaultMembershipProvider();               
member.CreateUser("New User", "password","[email protected]","First Pet","Rover",true,akey,out status)

I get an error with this stack trace:

Object reference not set to an instance of an object at System.Web.Providers.Entities.ModelHelper.CreateEntityConnection(ConnectionStringSettings setting, String csdl, String ssdl, String msl) at System.Web.Providers.Entities.ModelHelper.CreateMembershipEntities(ConnectionStringSettings setting) at System.Web.Providers.DefaultMembershipProvider.Membership_CreateUser(String applicationName, String userName, String password, String salt, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, DateTime& createDate, Boolean uniqueEmail, Int32 passwordFormat, Object& providerUserKey) at System.Web.Providers.DefaultMembershipProvider.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, Object providerUserKey, MembershipCreateStatus& status)

I figure it must be making the database connection OK as the authentication works. What am I doing wrong?

Just noticed that the Application property of member is still set to Null. Would've expected this to be "/" if it's reading it from the configuration. Maybe it isn't reading the configuration.

Just discovered that if I add the following it works:

config.Add("connectionStringName", "DefaultConnection");

            member.Initialize("DefaultMembershipProvider",config);

But I would've thought it should pick it up from the configuration file.

Upvotes: 2

Views: 1756

Answers (2)

Jerry Joseph
Jerry Joseph

Reputation: 1012

The mistake you did is that you created a new instance of DefaultMembershipProvider.

var member = new System.Web.Providers.DefaultMembershipProvider(); 
member.CreateUser("New User", "password", "[email protected]", "First Pet", "Rover", true, akey, out status)

This new instance does not read from the configurations you specify in the web.config. That is why you had to initialize this new instance and provide configurations to the instance.

Instead you should invoke the method in the static class directly as shown below.

Membership.CreateUser("New User", "password", "[email protected]", "First Pet", "Rover", true, akey, out status);

The settings will be loaded from the web.config automatically.

Upvotes: 0

Xorsat
Xorsat

Reputation: 2408

Check the connection string for ApplicationServices in web.config.

Upvotes: 2

Related Questions