Search code examples
asp.net-mvcreturn-type

return RedirectToAction not Functioning


enter image description here

I already simple checking the name and pass(=true)

When return RedirectToAction("Index", "Home"); ---> not function

I don't know what wrongs! No error show

Please help.. Thx a lot.


Solution

  • It is probably working.

    However, you most likely have Cookie authentication or similar activated which means that MVC will redirect the user back to the login page since the authentication cookie cannot be found.

    Before returning the redirect, you must create the cookie.

    How you do that depends on which version of MVC that you are running.

    Here is for MVC5: How to set a Auth cookie in asp.net 5

    Add the following before return RedirectToAction("Index", "Home");

       // Which claims depends on what kind of information you want to store about the user
       var claims = new[] { new Claim(ClaimTypes.Name, name) };
        var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
        Context.Authentication.SignIn(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
    

    And a ASP.NET Core MVC example is available here: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-3.1