Kaushik
Kaushik

Reputation: 2090

form authentication redirect page showing 401 error Mvc4 Empty Project

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 == "[email protected]" && 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

Upvotes: 0

Views: 538

Answers (1)

Fabio
Fabio

Reputation: 11990

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.

Upvotes: 1

Related Questions