Does not redirect to the expected view with jQuery Validation Plugin

0

I am using jQuery Validation Plugin to validate my login information, the problem is when in the url there is a subdomain, double sent the form or something like that.

The point is that instead of loading the view that I hope does not load and load the login, this is the login form:

<form action="@Url.Action(" Index ","Home ")" method="post" class="m-t" role="form" name="LoginForm">
  <!-- Ocultamos el input con el nombre del subdominio -->
  <input type="hidden" name="subdominio" value="@Model.subdominio" /> 
  @{ if (Model.subdominio != null) {
    <input type="hidden" value="@Model.rfcCompany" name="rfcCompany" id="rfcCompany" />
    <input type="hidden" value="@Model.nameCompany" name="nameCompany" id="nameCompany" />
    <input type="hidden" value="@Model.urlLogo" name="urlLogo" id="urlLogo" /> 
  } }
  <div class="form-group">
    <div class="input-group">
      <span class="input-group-addon b-r-md"><span class="glyphicon glyphicon-user"></span></span>
      <input type="email" name="email" class="form-control b-r-md" placeholder="Usuario">
    </div>
    <p class="help-block" id="LemailErr"></p>
  </div>
  <div class="form-group">
    <div class="input-group">
      <span class="input-group-addon b-r-md"><span class="fa fa-key"></span></span>
      <input type="password" name="portalPassword" class="form-control b-r-md" placeholder="Contraseña">
    </div>
    <p class="help-block" id="LportalPassword"></p>
  </div>
  <button type="submit" class="btn btn-primary block full-width m-b b-r-md">Entrar</button> 
  @{ if (Model.subdominio != null) {
    <a href="/RestaurarContraseña/RestaurarPass/EnviarCorreo/@Model.rfcCompany"><small>¿Olvidaste tu contraseña?</small></a> 
    if (Model.permitirAcceso > 1) {
      <p class="text-muted text-center"><small>¿No tienes una cuenta?</small></p>
      <a class="btn btn-sm btn-white btn-block" href="@Url.Action(" Nuevo ", "Registro ", new { area = "DescargaU " })/@Model.rfcCompany">Registrarse</a> 
    } 
  } }
</form>

This is the script to validate the form (I have already loaded the library in my view and it does not mark any error) :

<script>
  $('form[name="LoginForm"]').validate({
    //cambiando la posicion del mensaje de error
    errorPlacement: function(error, element) {
      error.appendTo(element.parent(".input-group").next("p"));
    },

    rules: {
      email: {
        required: true,
        email: true
      },
      portalPassword: {
        required: true,
        minlength: 6
      }
    },
    messages: {
      email: {
        required: "Ingresa tu Usuario",
        email: "Tu Usuario debe seguir este formato [email protected]"
      },
      portalPassword: {
        required: "No deje este campo vacio",
        minlength: jQuery.validator.format("Minimo {0} caracteres")
      }
    }
  });
</script>

This is the Home driver:

public ActionResult Index(string Subdominio)
{
  AdministrarModel model = new AdministrarModel();
  model.subdominio = Subdominio;
  return view(model);
}

[HttpPost]
public ActionResult Index(AdministrarModel model)
{
  LogOnClient webClient = new LogOnClient();
  AdministrarModel model = new AdministrarModel();
  LRDMResponse respuesta = null;
  LMNGRequest datosSession = new LMNGRequest()
  {
    AdmixProduct = LogOn.AdmixProduct.AdmixManagement,
    Username = model.email,
    Password = model.portalPassword,
    DeviceName = WSConfiguration.HostName,
    IpAddress = WSConfiguration.IpAddress,
    TypeDivice = LogOn.TypeDevice.Web
  };
  if (webClient.LoginManagement(datosSession, out respuesta))
  {
    sesionManage.cuenta = model.email.ToLower();
    sesionManage.devKey = respuesta.DeviceKey;
    sesionManage.mobileKey = respuesta.MobileKey;
    HttpContext.Session["sessionManage"] = sesionManage;
    return RedirectToAction("Apps", "MenuApps");//llega correctamente hasta aquí
  }else{
    model.error = "Usuario y contraseña incorrectos, intente nuevamente";
    return View(model);
  }
}

This is what it contains to capture the subdomain:

public class SubdomainRoute: RouteBase {
  public override RouteData GetRouteData(HttpContextBase httpContext) {
    if (httpContext.Request == null || httpContext.Request.Url == null) {
      return null;
    }

    var host = httpContext.Request.Url.Host;
    var index = host.IndexOf(".");
    string[] segments = httpContext.Request.Url.PathAndQuery.TrimStart('/').Split('/');

    if (index < 0) {
      return null;
    }

    var subdomain = host.Substring(0, index);
    string[] blacklist = {
      "www",
      "admix",
      "mail"
    };

    if (blacklist.Contains(subdomain)) {
      return null;
    }

    string controller = "Home";
    string action = "Index";

    var routeData = new RouteData(this, new MvcRouteHandler());
    routeData.Values.Add("controller", controller); //Goes to the relevant Controller  class
    routeData.Values.Add("action", action); //Goes to the relevant action method on the specified Controller
    routeData.Values.Add("Subdominio", subdomain); //pass subdomain as argument to action method
    return routeData;
  }

  public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) {
    //Implement your formating Url formating here
    return null;
  }
}
    
asked by JuankGlezz 24.04.2017 в 20:12
source

1 answer

1

I found the problem after intensive debugging both controllers and the capture of the subdomain.

The problem was that when accessing again the Index by post a request to the subdomain was opened and in this the request would not change, what I did was add these lines:

if (segments[0] != "")
{
  controller = segments[0];
  action = segments[1];
}

just after:

string controller = "Home";
string action = "Index";

In such a way that when opening the request it validates if a request has been opened to another view and not to index , and make the corresponding request.

Therefore the validation of the form works in the correct way without doing a double send to index

    
answered by 25.04.2017 / 00:06
source