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 == "[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

5
  • It seems that you are using the old forms authentication, while your application uses owin authentication. How is the authorize attribute configured? Commented Sep 3, 2015 at 19:06
  • I assume you are attempting to login as admin@gmail and using the specified password, correct? Commented Sep 3, 2015 at 19:07
  • @FabioLuz what is new way? what is owin authentication? Authrorize i got from using System.Web.Security; Commented Sep 3, 2015 at 19:09
  • @stephen.vakil that is not an issue. How can i resolve this? Commented Sep 3, 2015 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. Commented Sep 3, 2015 at 19:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Where I'll add code below this line To authenticate an user (usually inside a Login Action):
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.