evgeny
evgeny

Reputation: 62

ASP.NET MVC5 with existing Membership database

I have an existing ASP.NET Membership database which I have to stick to. I'm developing a new MVC5 website in VS2013 (update 4) using a new MVC website project template. I have modified web.config to ensure the old Membership type of authentication is specified. I have also modified generated [HttpPost]Login action to ensure I login against my Membership database - it logs me in correctly and generates an authentication cookie as required.

However the website still redirects me to the Login page as I'm not authenticated. The Request.IsAuthenticated does show that I'm not authenticated. What am I missing? What are my options? EDIT: web.config changes (only changes):

<connectionStrings>
  <add name="MyCS" connectionString="Data Source=sql1;Initial Catalog=MyDB;Persist Security Info=True;User ID=mysa;Password=mypw" providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
  <authentication mode="Forms">
    <forms timeout="20" name="seAdmin" loginUrl="~/Account/Login" />
  </authentication>
  <roleManager enabled="true" defaultProvider="CustomizedRoleProvider">
    <providers>
      <clear />
      <add name="CustomizedRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="MyCS" applicationName="/seAdmin" />
    </providers>
  </roleManager>
  <membership defaultProvider="CustomizedMembershipProvider">
    <providers>
      <clear />
      <add name="CustomizedMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="MyCS" applicationName="/seAdmin" />
    </providers>
  </membership>
  <machineKey validationKey="63A7C07B191BA3EF02DD4866C420DCAB81C9FFCCC617DE40ED6E2B89A2FC2BA3FA32C39D183FE0708E9279C14E58318D0C5E171C0AF802F154430679D1778485" decryptionKey="F5B9049DECB8C9A23B1D131E63D2ED5C15FF0AEB3C3E96FC" validation="SHA1" />
</system.web> 

Login action:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    if (Membership.ValidateUser(model.Email, model.Password))
    {
        FormsAuthentication.SetAuthCookie(model.Email, model.RememberMe);
        if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1)
        {
            return Redirect(returnUrl);
        }
        else
        {
            return RedirectToAction("Index", "Home");
        }
    }
    else
    {
        ModelState.AddModelError("", "The user name or password provided is incorrect.");
    }
    return View(model);
}

FilterConfig adds a global authorisation attribute

public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());

            filters.Add(new AuthorizeAttribute() { Roles = "CWA" });
        }
    }

Upvotes: 0

Views: 1024

Answers (1)

Arman Hamid Mosalla
Arman Hamid Mosalla

Reputation: 3355

I have some theories as to why it doesn't work, firstly in web.config there is a <system.webServer> tag, there must be a line for removing FormsAuthentication, try commenting it out, or add one again, like so:

<system.webServer>
<modules>
  <remove name="FormsAuthentication" />
  <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" />
</modules>

secondly try User.Identity.IsAuthenticated to see it is false too, also are you have any role in your app?

Upvotes: 2

Related Questions