0

I'm using MVC WebApi for creating an authentication, but when user is authenticated I try to send it to another view but for some reason it's not working and I don't know why

RoutConfig:

 public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Routing", action = "LogIn", id = UrlParameter.Optional }
        );
    }
}

Controller Code:

public class RoutingController : Controller
{
    //
    // GET: /Routing/
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Projects()
    {
        return View();
    }

    public ActionResult Users()
    {
        return View();
    }

    public ActionResult LogIn()
    {
        return View();
    }

    [HttpPost]
    public ActionResult LogInPost(string userName, string password)
    {
        User user = new User();
        RoleByUser rByU = new RoleByUser();
        password = UserController.EncriptPassword(password);
        var checkUser = user.Get(userName);
        var userExists = (from userInList in checkUser where userInList.UserName == userName && userInList.Password == password select userInList).FirstOrDefault();
        if(userExists!= null)
        {
            var roles = (from roleByUser in userExists.listOfRole select roleByUser.RoleName.Trim()).ToArray();
            IPrincipal principal = new GenericPrincipal(
            new GenericIdentity(userExists.UserName), roles);
            SetPrincipal(principal);
        }
        //Users();
        return RedirectToRoute("Users");
    }

    private void SetPrincipal(IPrincipal principal)
    {
        Thread.CurrentPrincipal = principal;
        if (System.Web.HttpContext.Current != null)
        {
            System.Web.HttpContext.Current.User = principal;
        }
    }

}

View Code:

@{
    ViewBag.Title = "LogIn";
}

<link href="~/css/Style.css" rel="stylesheet" type="text/css" />

<div class="container">
    <div class="card card-container">
        <img id="STK" class="profile-img-card" src="Images/Softtek.png" />
        <p id="profile-name" class="profile-name-card"></p>
        <form class="form-signin">
            <span id="reauth-email" class="reauth-email"></span>
            <input type="text" id="txtUserName" class="form-control" placeholder="Email address" required autofocus />
            <input type="password" id="txtPassword" class="form-control" placeholder="Password" required />
            <div id="remember" class="checkbox">
                <label>
                    <input type="checkbox" value="remember-me" /> Remember me
                </label>
            </div>
            <button id="btnLogIn" type="submit" class="btn btn-lg btn-primary btn-block btn-signin"  >Sing In</button>
        </form><!-- /form -->
        <a href="#" class="forgot-password">
            Forgot the password?
        </a>
    </div><!-- /card-container -->
</div><!-- /container -->



<script>
$(document).ready(function () {
    $('#btnLogIn').click(logIn);
});

function logIn() {
    $.ajax({
        type: "POST",
        url: "http://localhost:21294/Routing/LogInPost",
        dataType: "json",
        data: { userName: $('#txtUserName').val(), password: $('#txtPassword').val() },
        success: function () {

            toastr.success('Tus datos fueron agregados exitosamente!');
        },
        error: function (err, e, error) {
            toastr.error('Error')
        }
    });
}
</script>
Dai
  • 141,631
  • 28
  • 261
  • 374
user3442776
  • 162
  • 2
  • 4
  • 13
  • Note that semantically "redirect to another view" is incorrect, as you do not redirect to a "view", instead you redirect to an *action*, and the action returns a view. – Dai Oct 07 '15 at 20:10
  • Side note: There are no traces of WebAPI in the sample shown. Are you sure you've posted correct sample? – Alexei Levenkov Oct 07 '15 at 21:12

1 Answers1

1

The only route name you have registered is Default.

Try redirect to action instead:

return RedirectToAction("Users");
CainBot
  • 434
  • 2
  • 8
  • I already made and nothing happen – user3442776 Oct 07 '15 at 20:50
  • Hang on I just realised your issue. The login is an AJAX request. You cannot issue a redirect when responding to an AJAX request. I suggest you review the response from the server in the developer tools and go from there. Perhaps change it to a vanilla form post? Get back if you need help. – CainBot Oct 07 '15 at 20:55
  • I solved the issue, but not like I expected. I return the url and in the success function of the AJAX I made this: window.location.href = data; – user3442776 Oct 07 '15 at 21:12