0

I am trying to authenticate through WebSecurity class.

I have a form and when a user submits this is called:

@{
    if(IsPost) {
        var username = Request["username"];
        var password = Request["password"];

        WebSecurity.CreateAccount(username, password, false);

        WebSecurity.Login(username, password);

        Response.Redirect("Default.cshtml");
    }

}  

I already call:

WebSecurity.InitializeDatabaseConnection("Veidibok", "UserProfile", "UserId", "Email", true);

in my _appStart.cshtml file.

I get this error from the CreateAccount() call:

[MembershipCreateUserException: The Provider encountered an unknown error.]
WebMatrix.WebData.SimpleMembershipProvider.CreateAccount(String userName, String password, Boolean requireConfirmationToken) +1312
WebMatrix.WebData.WebSecurity.CreateAccount(String userName, String password, Boolean requireConfirmationToken) +31
ASP._Page_Authentication_Register_cshtml.Execute() in c:\Users\Notandi\Documents\My Web Sites\VeidiBok\Authentication\Register.cshtml:6
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68 System.Web.WebPages.WebPage.ExecutePageHierarchy() +156 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +119

So anyone have any ideas? I'm really new at this, just trying out WebMatrix and .cshtml

Damith
  • 62,401
  • 13
  • 102
  • 153
hallizh
  • 150
  • 1
  • 10

2 Answers2

7

Okay, this problem was a pain in my butt for the better part of today. I eventually had to check out the source code on Codeplex to get a better idea of what's happening (http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/ae31d2579b9a#src%2fWebMatrix.WebData%2fSimpleMembershipProvider.cs)

It seems that the CreateAccount primarily creates account information in the membership table but before it does that the method will check for the presence of a user with the same userName in the User profile table specified in the Initialize statement WebSecurity.InitializeDatabaseConnection

You can either call the CreateUser before you call the CreateAccount method or call the convenience method CreateUserAndAccount.

Mayur Birari
  • 5,837
  • 8
  • 34
  • 61
0

Solution:

You have create database Veidibok.sdf in DataBase workflow

Then

In Demo root folder create web.config file and add following code in connectionString area

<connectionStrings>
        <add name="Veidibok" connectionString="Data Source=|DataDirectory|\Veidibok.sdf;" />
    </connectionStrings>