gpt4 book ai didi

jquery - 如何从 Web Api .Net 中的 AuthorizeAttribute 检索 POST 参数?

转载 作者:行者123 更新时间:2023-12-01 07:42:19 25 4
gpt4 key购买 nike

我正在为我的 Web Api .Net 项目开发一种使用 token 的身份验证方法,因此我将重写一些如下方法:

public class Authorizetest: System.Web.Http.AuthorizeAttribute
{
public override void OnAuthorization(HttpActionContext actionContext)
{
if(Authorize(actionContext))
{
return;
}
HandleUnauthorizedRequest(actionContext);
}

protected override void HandleUnauthorizedRequest(HttpActionContext actionContext)
{
base.HandleUnauthorizedRequest(actionContext);
}

private bool Authorize(HttpActionContext actionContext)
{
try
{
var context = new HttpContextWrapper(HttpContext.Current);
HttpRequestBase request = context.Request;
string token = request.Params["Token"];
return true;
}
catch (Exception)
{
return false;
}
}
}

我以这种方式使用装饰器[Authorizetest]:

[Authorizetest]
public class DoActionController : ApiController
{
[HttpPost]
public Display DoSomething(Parameter param)
{
//do something
return display;
}
}

但是 request.Params 返回 null 但是在 DoSomething 方法中我从 Parameter 获取值。

我也尝试过类似的方法:(基于 this page )

    HttpRequestBase request = actionContext.RequestContext.HttpContext.Request;
string token = request.Params["Token"];

,但无法检索通过 POST 方法发送的任何值。

我正在使用 JQuery 发送数据

$.ajax({
type: 'POST',
url: '/DoSomething',
data: JSON.stringify({ "Token": "xxxxxxxxx"}),
contentType: 'application/json; charset=utf-8',
success: function (data) {
},
fail:function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});

如何检索发送到 Authorizetest 类 中的 DoSomething 的数据?

最佳答案

如果打算使用授权属性进行授权,则应在请求 header 中发送身份验证 token ,然后通过授权属性提取身份验证 token 。在模型绑定(bind)器有机会填充模型之前读取请求正文可能会产生负面影响。

var token = "xxxxxxxxx";
$.ajax({
type: 'POST',
url: '/DoSomething',
data: JSON.stringify({ "SomeProperty": "SomeValue"}),
contentType: 'application/json; charset=utf-8',
beforeSend: function (xhr) {
/* Authorization header */
xhr.setRequestHeader("Authorization", "Token " + token);
},
success: function (data) {
},
fail:function (XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});

然后在服务器上访问

public class Authorizetest : System.Web.Http.AuthorizeAttribute {
public override void OnAuthorization(HttpActionContext actionContext) {
if (Authorize(actionContext)) {
return;
}
HandleUnauthorizedRequest(actionContext);
}

protected override void HandleUnauthorizedRequest(HttpActionContext actionContext) {
base.HandleUnauthorizedRequest(actionContext);
}

private bool Authorize(HttpActionContext actionContext) {
try {
var auth = actionContext.Request.Headers.Authorization;
if (auth != null) {
var scheme = auth.Scheme; //Should be Token, otherwise fail
var token = auth.Parameter;
//Validate your token and set your principal
IPrincipal user = GetUser(token);
if (user != null) {
SetPrincipal(user);
return true;
}
}
return false;
} catch (Exception) {
return false;
}
}

private IPrincipal GetUser(string token) {
throw new NotImplementedException(); //Put your implementation here
}

private void SetPrincipal(System.Security.Principal.IPrincipal principal) {
if (principal != null) {
System.Threading.Thread.CurrentPrincipal = principal;
if (System.Web.HttpContext.Current != null) {
System.Web.HttpContext.Current.User = principal;
}
}
}
}

关于jquery - 如何从 Web Api .Net 中的 AuthorizeAttribute 检索 POST 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45555502/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com