指向简单成员资格提供程序以使用MongoDB连接

时间:2014-06-02 17:37:44

标签: asp.net asp.net-mvc mongodb asp.net-mvc-4 simplemembership

在MVC4应用程序中,我将简单成员资格提供程序指向MongoDB Connection。但是连接字符串会引发错误。

这是我的代码

的Web.config

<add name="DefaultConnection" connectionString="server=127.0.0.1;database=user" />
 <membership defaultProvider="DefaultMembershipProvider">
  <providers>
    <add name="DefaultMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
</membership>
<roleManager enabled="true" defaultProvider="DefaultRoleProvider">
  <providers>
    <add name="DefaultRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
</roleManager>

Global.asax中

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "Users", "Id", "Username", autoCreateTables: true);

SimpleMembershipProvider

 public class SimpleMongoMembershipProvider : SimpleMembershipProvider
 {
    public override string CreateAccount(string userName, string password)
    {
        WebSecurity.CreateUserAndAccount(userName,password, new { Gender = "Mal", DOB = DateTime.Now.AddYears(-1), Email = "mymy@trtr.com" });
        return base.CreateAccount(userName, password);
    }
 }

帐户控制器

public class AccountController : Controller
{
    public ActionResult Index()
    {
        var db = new SimpleMongoMembershipProvider();
        db.CreateAccount("admin", "admin");

        return View();
    }
}

有什么想法吗?需要遵循任何其他步骤将MongoDB指向Connection

1 个答案:

答案 0 :(得分:0)

您似乎没有对Mongo DB进行身份验证或传递凭据,您需要对Mongo数据库进行身份验证,根据您的使用情况,有几种不同的方法可以执行此操作,请使用此处列出的说明: http://docs.mongodb.org/ecosystem/tutorial/authenticate-with-csharp-driver/