ASP.NET MVC-用于验证POST数据的ActionFilterAttribute [英] ASP.NET MVC - ActionFilterAttribute to validate POST data

查看:393
本文介绍了ASP.NET MVC-用于验证POST数据的ActionFilterAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我有一个使用WebService检索某些客户端信息的应用程序. 因此,我正在验证ActionResult内部的登录信息,例如:

Actually I have an application that is using a WebService to retrieve some clients information. So I was validating the login information inside my ActionResult like:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ClientLogin(FormCollection collection)
{
    if(Client.validate(collection["username"], collection["password"]))
    {
        Session["username"] = collection["username"];
        Session["password"] = collection["password"];
        return View("valid");
    }
    else
    {
       Session["username"] = "";
       Session["password"] = "";
       return View("invalid");
    }
}

其中Client.Validate()是一种根据POST用户名和密码提供的信息返回布尔值的方法

Where Client.Validate() is a method that returns a boolean based on the information provided on the POST username and password

但是我改变了主意,我想在该方法的开头使用该不错的ActionFilterAttributes,以便仅在Client.validate()返回true时才呈现它,与[Authorize]相同,但使用我的自定义网络服务,所以我会有类似的东西:

But I changed my mind and I would like to use that nice ActionFilterAttributes at the beginning of the method so it will just be rendered if the Client.validate() return true, just the same as [Authorize] but with my custom webservice, so I would have something like:

[AcceptVerbs(HttpVerbs.Post)]
[ValidateAsClient(username=postedUsername,password=postedPassword)]
//Pass Posted username and password to ValidateAsClient Class
//If returns true render the view
public ActionResult ClientLogin()
{
    return View('valid')
}

,然后在ValidateAsClient中,我将得到类似的内容:

and then inside the ValidateAsClient I would have something like:

public class ValidateAsClient : ActionFilterAttribute
{
    public string username { get; set; }
    public string password { get; set; }

    public Boolean ValidateAsClient()
    {
        return Client.validate(username,password);
    }
}

所以我的问题是,我不确切地知道如何使其工作,因为我不知道如何将POSTED信息传递给 [ValidateAsClient(username = postedUsername,password = postedPassword)] ,而且如何使ValidateAsClient函数正常工作?

So my problem is, I don't know exactly how to make it work, because I don't know how to pass the POSTED information to the [ValidateAsClient(username=postedUsername,password=postedPassword)] and also, how could I make the function ValidateAsClient work properly?

我希望这很容易理解 预先感谢

I hope this is easy to understand Thanks in advance

推荐答案

类似这样的事情:

[AttributeUsage(AttributeTargets.All)]
public sealed class ValidateAsClientAttribute : ActionFilterAttribute
{
    private readonly NameValueCollection formData;
    public NameValueCollection FormData{ get { return formData; } }

    public ValidateAsClientAttribute (NameValueCollection formData)
    {
        this.formData = formData;
    }

    public override void OnActionExecuting
               (ActionExecutingContext filterContext)
    {
        string username = formData["username"];
        if (string.IsNullOrEmpty(username))
        {
             filterContext.Controller.ViewData.ModelState.AddModelError("username");
        }
        // you get the idea
    }
}

并像这样使用它:

[ValidateAsClient(HttpContext.Request.Form)]

这篇关于ASP.NET MVC-用于验证POST数据的ActionFilterAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆