Custom MVC 4 Membership Provider Template

What I want to do is use the default MVC template that just comes with Visual Studio 2012 as the base for my new project. However, I want to replace the SQL provider with a custom membership provider so that I can access my RavenDB to get my users. I implemented a custom provider as before, but WebSecurity methods throw the following exception.

This line of code:

ViewBag.HasLocalPassword = OAuthWebSecurity.HasLocalAccount (WebSecurity.GetUserId (User.Identity.Name));

In particular, the method:

WebSecurity.GetUserId

Throws this exception:

You must call the "WebSecurity.InitializeDatabaseConnection" method before you call any other method of the "WebSecurity" class. This call should be placed in the _AppStart.cshtml file in the root of your site.

Now I can not call InitializeDatabaseConnection, because my provider is not a SQL provider. This method expects the SQL provider and SQL connection string. Is this a common problem or am I missing something? Why should WebSecurity be initialized and why does it only wait for connectivity using the SQL provider?

Do I just need to change the code to not use the WebSecurity class?

I have been here all day and am pretty tired. I hope I haven’t missed anything simple. Maybe another rum and coke will help ...

Update: 08/19/2012

GetUserId , , , - VerifyProvider.

public static int GetUserId(string userName)
{
    WebSecurity.VerifyProvider();
    MembershipUser user = Membership.GetUser(userName);
    if (user == null)
        return -1;
    else
        return (int) user.ProviderUserKey;
}

private static ExtendedMembershipProvider VerifyProvider()
{
    ExtendedMembershipProvider membershipProvider = Membership.Provider as ExtendedMembershipProvider;

    if (membershipProvider == null)
        throw new InvalidOperationException(WebDataResources.Security_NoExtendedMembershipProvider);

    membershipProvider.VerifyInitialized();
    return membershipProvider;
}

, VerifyProvider, - VerifyInitialized, . , , , VerifyInitialized.

internal virtual void VerifyInitialized()
{
}

Web.Config. , . .

<membership defaultProvider="RavenMembershipProvider">
    <providers>
        <clear />
        <add name="RavenMembershipProvider" type="BigGunsGym.Infrastructure.Providers.RavenMembershipProvider" />
    </providers>
</membership>
+5
2

, , SimpleMembershipProvider ExtendedMembershipProvider. , , SimpleMembershipProvider ExtendedMembershipProvider, .

ExtendedMembershipProvider .

+5

, ....

...

public ActionResult CreateUsers()
{
string username = "blah blah auto create";
string password = "blah blah auto create";
WebSecurity.CreateUserAndAccount(username, password);
}

,

[InitializeSimpleMembership]
public ActionResult CreateUsers()
{
string username = "blah blah auto create";
string password = "blah blah auto create";
WebSecurity.CreateUserAndAccount(username, password);
}
+1

All Articles