我们的应用程序(基于MVC)通过GET方法接受用户支付信息更新请求。应用程序使用的默认方法是POST。 目前,如果我们通过Querystring通过GET方法传递任何敏感信息,那么Request for sucessfully工作。原因是它在Controller中遇到了相同的Edit Action方法
[HttpGet]
[ValidateRequest(true)]
public ActionResult Edit (parameters)
但我们想要的是,应用程序会拒绝任何通过GET方法发送的具有敏感信息(如信用卡等)的请求。 无论如何,如果传递敏感信息,我们可以通过路由拒绝GET方法吗?请建议有效的方法。
我现在称之为Action的路线如下:
routes.MapRoute("ChargeInformation", "ChargeInformationt.aspx/{seq}", new { controller = "Payment", action = "Edit", seq = UrlParameter.Optional });
答案 0 :(得分:0)
路由的唯一责任是将URL映射到路由值,并将路由值映射回URL。与授权请求相比,这是一个单独的问题。实际上,内置路由扩展方法(MapRoute
,MapPageRoute
和IgnoreRoute
)完全忽略了传入的查询字符串。
对于请求授权,MVC有一个IAuthorizationFilter
接口可以挂钩。您还可以(可选)将其与属性组合,以使其在特定操作方法上有条件地运行,如下所示。
在这种情况下,您只想拒绝传递给请求的特定查询字符串键名。目前还不清楚你希望在这种情况下采取什么行动,所以我只是禁止使用HTTP 403作为例子。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Mvc;
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class DisallowQueryStringKeysAttribute : FilterAttribute, IAuthorizationFilter
{
private readonly IEnumerable<string> keysSplit;
public DisallowQueryStringKeysAttribute(string keys)
{
this.keysSplit = SplitString(keys);
}
public void OnAuthorization(AuthorizationContext filterContext)
{
var queryStringKeys = filterContext.HttpContext.Request.QueryString.AllKeys;
// If any of the current query string keys overlap with the non-authorized keys
if (queryStringKeys.Intersect(this.keysSplit, StringComparer.OrdinalIgnoreCase).Any())
{
filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
// You must set the result property to a handler to run to tell the
// framework that the filter should do something other than run the
// action method. In this case, we just set it to an empty result,
// which implements the null object pattern. You could (if so inclined),
// make a class to set the status code or do something else
// (such as redirect) to indicate that the request is invalid.
filterContext.Result = new EmptyResult();
}
}
private string[] SplitString(string original)
{
if (String.IsNullOrEmpty(original))
{
return new string[0];
}
var split = from piece in original.Split(',')
let trimmed = piece.Trim()
where !String.IsNullOrEmpty(trimmed)
select trimmed;
return split.ToArray();
}
}
[HttpGet]
[ValidateRequest(true)]
[DisallowQueryStringKeys("creditCard, password")]
public ActionResult Edit (string creditCard, string password)