0

I had tried - RedirectToAction - Redirect - Url.Action

I'm creating an application form authentication using mvc4 empty project.

Here is code for login

[HttpPost]

[AllowAnonymous]
public ActionResult Index(UserModel user, string returnUrl)
{
    try
    {
        string EmailAddress = user.EmailAddress;
        string Password = user.Password;

        if (!string.IsNullOrEmpty(EmailAddress) && !string.IsNullOrEmpty(Password) && EmailAddress == "admin@gmail.com" && Password == "admin123")
        {
            FormsAuthentication.SetAuthCookie(EmailAddress, true); 
            // Result = new { Status = "Success" };
            FormsAuthentication.RedirectFromLoginPage(EmailAddress, true);
            return Redirect(Url.Action("Index","Home"));

        }
        else
        {
            return RedirectToAction("Index");
        }
    }
    catch (Exception)
    {
        // logging
        throw;
    }
}

}

My FilterConfig

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

I'm new to this section. I'm wondering why it is happening?

Screenshot of response after redirection to home page.

Error Page on redirect action

Kaushik
  • 2,072
  • 1
  • 23
  • 31
  • It seems that you are using the old forms authentication, while your application uses owin authentication. How is the authorize attribute configured? – Fabio Sep 03 '15 at 19:06
  • I assume you are attempting to login as admin@gmail and using the specified password, correct? – stephen.vakil Sep 03 '15 at 19:07
  • @FabioLuz what is new way? what is owin authentication? Authrorize i got from `using System.Web.Security;` – Kaushik Sep 03 '15 at 19:09
  • @stephen.vakil that is not an issue. How can i resolve this? – Kaushik Sep 03 '15 at 19:09
  • @KaushikKishore Take a look at your solution, look for the file AuthConfig.cs (inside App_Start folder) and Startup.cs inside root folder. Did you find these files? if yes, post them in your question. – Fabio Sep 03 '15 at 19:11

1 Answers1

1

I believe the Authorization is not working because you are trying to use the old FormsAuthentication method, while you are using the new AuthorizeAttribute that uses OWIN authentication.

Follow this question MVC Authentication - Easiest Way. It should solve your problem.

Community
  • 1
  • 1
Fabio
  • 11,892
  • 1
  • 25
  • 41
  • Where I'll add code below this line `To authenticate an user (usually inside a Login Action):` – Kaushik Sep 03 '15 at 19:26
  • In the same place your are using `FormsAuthentication.SetAuthCookie(EmailAddress, true);`. In your case, inside Index Action. (You have to remove the 2 FormsAuthentication Line) – Fabio Sep 03 '15 at 19:28