创建用户时出错 - ASP.NET System.Web.Providers
我正在使用新的 ASP.NET 通用提供程序,如下所述 Hanselman 博客文章评论:
我可以使用以下命令将其连接起来进行身份验证:
<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>
然后,我尝试使用以下代码创建一个新用户,
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)
但我收到以下堆栈跟踪错误:
未将对象引用设置为对象的实例 在 System.Web.Providers.Entities.ModelHelper.CreateEntityConnection(ConnectionStringSettings 设置,字符串 csdl,字符串 ssdl,字符串 msl) 在 System.Web.Providers.Entities.ModelHelper.CreateMembershipEntities(ConnectionStringSettings 设置) 在System.Web.Providers.DefaultMembershipProvider.Membership_CreateUser(字符串applicationName,字符串用户名,字符串密码,字符串盐,字符串电子邮件,字符串密码Question,字符串密码Answer,布尔值isApproved,日期时间和createDate,布尔值uniqueEmail,Int32密码格式,对象和providerUserKey) 在 System.Web.Providers.DefaultMembershipProvider.CreateUser(字符串用户名,字符串密码,字符串电子邮件,字符串密码问题,字符串密码答案,布尔值 isApproved,对象提供者用户密钥,MembershipCreateStatus 和状态)
我认为它必须使数据库连接正常,因为身份验证有效。 我做错了什么?
刚刚注意到成员的 Application 属性仍然设置为 Null。如果从配置中读取它,则预计它是“/”。也许它没有读取配置。
刚刚发现,如果我添加以下内容,它就可以工作:
config.Add("connectionStringName", "DefaultConnection");
member.Initialize("DefaultMembershipProvider",config);
但我认为它应该从配置文件中获取它。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
检查 web.config 中
ApplicationServices
的连接字符串。Check the connection string for
ApplicationServices
in web.config.您犯的错误是创建了 DefaultMembershipProvider 的新实例。
此新实例不会读取您在 web.config 中指定的配置。这就是为什么您必须初始化这个新实例并向该实例提供配置。
相反,您应该直接调用静态类中的方法,如下所示。
设置将自动从 web.config 加载。
The mistake you did is that you created a new instance of DefaultMembershipProvider.
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.
The settings will be loaded from the web.config automatically.