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","new开发者_如何学运维User@newuser.com","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.
Check the connection string for ApplicationServices
in web.config.
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", "newUser@newuser.com", "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", "newUser@newuser.com", "First Pet", "Rover", true, akey, out status);
The settings will be loaded from the web.config automatically.
精彩评论