简体   繁体   English

如何在Asp.Net MVC上实现客户端Ajax登录(Asp.Net Webforms解决方案的链接在这里)

[英]How to implement a Client-side Ajax Login on Asp.Net MVC (A link to the solution for Asp.Net Webforms is in here)

I'm trying to implement a client-side ajax login on Asp.Net MVC. 我正在尝试在Asp.Net MVC上实现客户端ajax登录。 I used to have this set up just fine on WebForms, but now that I've moved to MVC it's giving me some troubles. 我以前在WebForms上设置得很好,但是现在我已经转移到MVC了,它给了我一些麻烦。

If you'd like a tutorial on Client-side Ajax Login for Asp.Net Webforms, it can be found here -- Easy, A++ 如果你想要一个关于Asp.Net Webforms的客户端Ajax登录的教程,可以在这里找到 - Easy,A ++

Now... for some reason it's not working for Asp.Net MVC. 现在......出于某种原因,它不适用于Asp.Net MVC。

I used the exact same tutorial as for the Webforms, except when it executes the ssa.login() (equivalently: Sys.Services.AuthenticationService.login() ) it's not doing anything. 我使用与ssa.login()完全相同的教程,除非它执行ssa.login() (相当于: Sys.Services.AuthenticationService.login() )它没有做任何事情。

I have alerts in both the onLoginComplete() function and the onError() function. 我在onLoginComplete()函数和onError()函数中都有警报。 As well I have an alert before the ssa.login gets called and right after... 同样,我在ssa.login被调用之前有一个警报,然后......

function loginHandler() {
    var username = $("#login_UserName").val();
    var password = $("#login_Password").val();
    var isPersistent = $("#login_RememberMe").attr("checked");
    var customInfo = null;
    var redirectUrl = null;
    // Log them in.
    alert("try login");
    ssa.login(username,
                      password,
                      isPersistent,
                      customInfo,
                      redirectUrl,
                      onLoginComplete,
                      onError);
    alert("made it here");
}

The first alert fires but the second one doesn't which means the function is failing. 第一个警报触发但第二个警报触发,这意味着该功能失败。
Here's the function I pulled from Asp.Net Ajax to show you: 这是我从Asp.Net Ajax中提取的函数向您展示:

function(c, b, a, h, f, d, e, g) {
    this._invoke(this._get_path(), "Login", false, { userName: c, password: b, createPersistentCookie: a }, Function.createDelegate(this, this._onLoginComplete), Function.createDelegate(this, this._onLoginFailed), [c, b, a, h, f, d, e, g]);
}

Anyone have any idea of why it's failing? 任何人都知道为什么会失败?

You are making this more complicated than it needs to be. 你使它变得比它需要的更复杂。 All you need to do is call your Account/Login method with the AJAX call. 您需要做的就是使用AJAX调用调用您的帐户/登录方法。 You don't need the complication of the Authentication service, though you probably want to detect whether you are logging in via AJAX and return JSON rather than a View. 您不需要验证服务的复杂性,但您可能想要检测是否通过AJAX登录并返回JSON而不是View。

function loginHandler() {
    var username = $("#login_UserName").val();
    var password = $("#login_Password").val();
    var isPersistent = $("#login_RememberMe").attr("checked");
    var customInfo = null;
    var redirectUrl = null;
    // Log them in.
    alert("try login");
    $.ajax( {
       url : '<%= Url.Action( "Login", "Account" ) %>',
       type: 'post',
       dataType: 'json',
       data: { username: username,
               password: password,
               isPersistent: isPersistent,
              },
       success: onLoginComplete,
       error: onError
    });
    alert("made it here");  // this will execute before the callback completes...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM