PostgreSQL
 sql >> Base de Dados >  >> RDS >> PostgreSQL

Asp.net MVC4, PostgreSQL e SimpleMembership


O problema está na implementação de um AccountController em vez do padrão.

OK. Aqui está a solução

Gosto deste Membership and Role Providers for PostgreSQLhttps://code.google.com/p/pgprovider / porque cria todas as tabelas necessárias por si só quando inicia))

Instale-o:

Install-Package pgProvider

Adicione ao Web.config:
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net"/>
  </configSections>
  <connectionStrings>
    <add name="MovieDBContext" connectionString="Server=127.0.0.1;Port=5432;Database=postgres;User Id=postgres;Password=000;" providerName="Npgsql" />
    <add name="pgProvider" connectionString="Server=127.0.0.1;Port=5432;Database=postgres;User Id=postgres;Password=000;" providerName="Npgsql" />    
  </connectionStrings>

    <membership defaultProvider="pgMembershipProvider">
      <providers>
        <clear />
        <add name="pgMembershipProvider" type="pgProvider.pgMembershipProvider, pgProvider" applicationName="MvcMovie"/>
      </providers>
    </membership>
    <roleManager defaultProvider="pgRoleProvider">
      <providers>
        <clear />
        <add name="pgRoleProvider" type="pgProvider.pgRoleProvider, pgProvider" applicationName="MvcMovie"/>
      </providers>
    </roleManager>

Em seguida, adicione ao Web.config também:
  <appSettings>
    //---
    <add key="enableSimpleMembership" value="false"/>
    <add key="autoFormsAuthentication" value="false"/>    
  </appSettings>

Em seguida, exclua AccountController criado por padrão.

E por fim adicione o AccountController:
public class AccountController : Controller 
{
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;        
        return View();
    }

[HttpPost]
public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {
        if (Membership.ValidateUser(model.UserName, model.Password))
        {
            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        else
        {
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
        }
    }
    return View(model);
}

public ActionResult LogOff()
{
    FormsAuthentication.SignOut();

    return RedirectToAction("Login", "Account");
}

public ActionResult Register()
{
    return View();
}

[HttpPost]
public ActionResult Register(RegisterModel model)
{           
    if (ModelState.IsValid)
    {
        MembershipCreateStatus status;

        MembershipUser membershipUser = (Membership.Provider).CreateUser(
            model.UserName, model.Password, null,null,null, true, null, out status);

        if (membershipUser != null)
        {
            FormsAuthentication.SetAuthCookie(model.UserName, false);
            return RedirectToAction("Index", "Home");
        }
        else
        {
            ModelState.AddModelError("", "Registration error");
        }
    }
    return View(model);
}

}

O código-fonte do provedor está aqui:https:/ /code.google.com/p/pgprovider/source/browse/trunk/pgProvider/pgMembershipProvider.cs

Isso é tudo.

Funciona com as visualizações Login e Register, criadas por padrão.