我感兴趣的是如何进行MVC WEB API自动化。我检查了basic authentication,但我有不同的情况。在我的情况下,登录参数被期望作为一个动作参数,而不是在标题内。
namespace Test.Controllers
{
public class TestController : Controller
{
[RequireHttps]
[Authorize]
public void TestRequest(int actionParam, string username, string token, int appID)
{
something.......
}
}
}
我也发现了这个解释http://www.codeproject.com/Tips/867071/WebAPI-Security-Custom-Authorization-Filters,但想知道是否可以从授权中访问操作参数而不是标头值?
答案 0 :(得分:3)
只需通过OnAuthorization
或HttpActionContext
覆盖HttpContext.Current.Request
覆盖中的查询字符串参数:
请参阅:How to get Request Querystring values?
public override void OnAuthorization(HttpActionContext actionContext)
{
var queryString = HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query.Substring( 1 ));
var username = queryString["username"];
}
或参见:Accessing QueryString in a custom AuthorizeAttribute
然后添加using System.Web;
:
public override void OnAuthorization(HttpActionContext actionContext)
{
var username = HttpContext.Current.Request.QueryString["username"];
}