0

I am trying create a login page that should redirect to an action result on successful login. I have user roles that should check. For now I can validate user but it does not redirect to Authorize Action result. Besides it provide return url to login page. Here is my Code for Account Controller

public ActionResult Login()
    {
        return View();
    }
    [HttpPost]
    public ActionResult Login(string UserName, string Password)
    {
        if (new UserManager().IsValid(UserName, Password))
        {
            string userName = "Test Name"; //Hard coded for test purpose
            string[] userRoles = {"Admin"};//Hard coded for test purpose

            ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);

            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userName));

            userRoles.ToList().ForEach((role) => identity.AddClaim(new Claim(ClaimTypes.Role, role)));

            identity.AddClaim(new Claim(ClaimTypes.Name, userName));

            AuthenticationManager.SignIn(identity);
            return RedirectToAction("Index","Home"); // auth succeed 
            // auth succeed 
        }
        // invalid username or password
        ModelState.AddModelError("", "invalid username or password");
        return View();

    }

    private IAuthenticationManager AuthenticationManager
    {
        get { return HttpContext.GetOwinContext().Authentication; }
    }

And Home controller

[Authorize]     
    public ActionResult Index()
    {
        return View(); //Which contain Dashboard for admin panel  
    }

But problem is when it appear to return RedirectToAction("Index","Home");it not redirect but URL change to ../Account/Login?ReturnUrl=%2fHome. This is very new to me (Past I restrict user from database), Is there anything wrong with this code? Thanks in advance....

Parvez
  • 187
  • 1
  • 2
  • 18
  • *it not redirect* - What does it do? – NightOwl888 Feb 26 '18 at 18:15
  • @NightOwl888 When it reached to 'return RedirectToAction("Index","Home");' It not redirect to Home controller/Action (test by debugger) rather than its Return Url to login page – Parvez Feb 26 '18 at 18:29
  • 1
    That indicates that your authentication isn't working or isn't properly registered with MVC. Your code looks a lot like [this answer](https://stackoverflow.com/q/40306141). Did you read the comments about missing OWIN NuGet packages? – NightOwl888 Feb 26 '18 at 18:40
  • Yes that answer I modified. I install all the packages from nuget but still same problem. It's only redirect when I use `[ValidateAntiForgeryToken]` with `[Authorize]` on Home/Index. But this redirect cause an error says **The required anti-forgery form field "__RequestVerificationToken" is not present.** @NightOwl888 – Parvez Feb 26 '18 at 18:49
  • add `@Html.AntiForgeryToken()` to your form to sort out the Request Validation Token issue – Tom John Feb 26 '18 at 20:37
  • 1
    I think there is some problem with my identity authorization. Do I have add something to webconfig or startup.cs (Based from some search)? @TomJohn – Parvez Mar 03 '18 at 08:46
  • @Parvez was this ever solved? We ran into a similar issue this week and have no idea why RedirectToAction forces us back to Account/Login. – uchamp Jul 28 '20 at 14:26

0 Answers0