jQuery的窗体身份验证的ASP

jQuery的窗体身份验证的ASP

本文介绍了jQuery的窗体身份验证的ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用jQuery的Ajax调用与ASP.NET MVC执行表单验证?我一直无法找到任何这样的例子。

Is it possible to use a jQuery ajax call to perform Forms Authentication with ASP.NET MVC? I've been unable to find any such examples.

更具体地说,如何设置页面上的权威性的cookie(没有重定向),所以我可以连续验证Ajax请求?

More specifically, how do I set the auth cookie on the page (without a redirect) so I can make successive authenticated ajax requests?

推荐答案

是的,这是可能的。只要使用方法described这里由Mike博世并返回一个JSON数据结构与RETURNURL(如有)。

Yes, it's possible.Just submit the login-form using the method described here by mike bosch and return a json datastructure with the returnUrl if any.

我已经创建了我返回JSON作为一个轻量级的LoginResultDTO类:

I have created a lightweight LoginResultDTO class that i return as json:

public class LoginResultDTO
{
  public bool Success {get;set;}
  public string Message {get;set;}
  public string ReturnUrl {get;set;}
}

下面是从了登录视图脚本块:

Here's a script block from my LogOn view:

<script type="text/javascript">
        $(document).ready(function() {
            var form = $($("form")[0]);
            form.submit(function() {
                var data = form.serialize();
                $.post(form.attr("action"), data, function(result, status) {
                    if (result.Success && result.ReturnUrl) {
                            location.href = result.ReturnUrl;
                    } else {
                        alert(result.Message);
                    }
                }, "json");
                return false;
            });
        });
</script>

这将阿贾克斯包裹登录表单。请注意,这是JavaScript的code最简单的实现可能的,但它是一个开始的地方。

This will ajax wrap the logon form. Note that this is the simplest implementation of the javascript code possible but it's a place to start.

然后我在的AccountController,并在相关的地方修改了登录的动作把这样的事情:

Then I have modified my LogOn action in the AccountController and in the relevant places put something like this:

if(Request.IsAjaxRequest())
{
  return Json(new LoginResultDTO{Success=true,Message="Successfully logged in"});
}else
{
  return View();
}

所以这是一个超轻,但如何jQuery的认证在asp.net mvc的可以做的比较完整的版本。

So this is an ultralight but rather complete version of how jquery authentication could be done in asp.net mvc.

这篇关于jQuery的窗体身份验证的ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 01:45