编辑:我找到了解决问题的方法。更多信息在帖子末尾。
我构建了一个 WCF 服务,该服务使用会员资格为我的客户提供授权并管理用户/角色等。到目前为止,授权效果很好。
我试图让我的客户能够注册新用户,所以我决定从我的服务中接口 sql 成员资格提供程序代码,然后从客户端调用这些方法,因为我还没有看到直接调用的方法来自没有客户端应用程序服务的客户端的 SQL 提供程序。
我尝试在服务中连接 CreateUser 方法,当我从客户端调用它以创建新用户时,我收到此错误并且无法弄清楚出了什么问题:
你调用的对象是空的。
这是我的服务中直接调用会员提供程序的代码。
public string RegisterUser(string username, string password, string email,
string passwordquestion, string passwordanswer, bool isapproved)
{
MembershipCreateStatus status;
SqlMembershipProvider mProvider = new SqlMembershipProvider();
mProvider.CreateUser(username, password, email, passwordquestion, passwordanswer, true, null, out status);
switch (status)
{
case MembershipCreateStatus.DuplicateUserName:
return "Username already exists. Please enter a different user name.";
case MembershipCreateStatus.DuplicateEmail:
return "A username for that e-mail address already exists. Please enter a different e-mail address.";
case MembershipCreateStatus.InvalidPassword:
return "The password provided is invalid. Please enter a valid password value.";
case MembershipCreateStatus.InvalidEmail:
return "The e-mail address provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.InvalidAnswer:
return "The password retrieval answer provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.InvalidQuestion:
return "The password retrieval question provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.InvalidUserName:
return "The user name provided is invalid. Please check the value and try again.";
case MembershipCreateStatus.ProviderError:
return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
case MembershipCreateStatus.UserRejected:
return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
default:
return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
}
}
这是我从客户那里调用的代码:
private void CreateUser_Click(object sender, RoutedEventArgs e)
{
wsBasicAuthService.BasicAuthService c = new wsBasicAuthService.BasicAuthService();
c.Credentials = new NetworkCredential(txtUserName.Text, txtPassword.Password);
c.RegisterUser("myUserName", "password", "email@test.com", "question", "answer", true, true);
}
我注意到它本身向 c.RegisterUser 参数调用“isApprovedSpecified”添加了一个额外的参数,尽管我认为这与它没有任何关系。
我是会员和服务的新手,很难从 vb.net 切换到 C#,所以如果有人能引导我朝着正确的方向前进,我将不胜感激。
解决方案:在 web.config 文件中将 includeExceptionDetailInFaults 设置为 true 后,我能够快速找出这是一个对象引用问题。所以我认为会员提供者一定不能被正确实例化。就是这样。这是它应该看起来的样子。
SqlMembershipProvider mProvider = (SqlMembershipProvider)Membership.Providers["membershipProvider"];
//membershipProvider is the name you have configured for your provider in the web.config file.