如何在 ASP.NET 中的自定义授权属性中获取引用数据,如 IP 地址和 apiKey。在下面的代码中,我想获取上面提到的数据:
[AttributeUsage(AttributeTargets.Method)]
public class Auth : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
var cnt = HttpContext.Current;
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
在 HttpContext.Current 中,我可以获得 url 引用,但它没有我需要的所有数据。
如果这个问题不清楚或不完整,请告诉我。
更新:对于那些需要知道的人,下面的代码显示了如何。
[AttributeUsage(AttributeTargets.Method)]
public class Auth : AuthorizeAttribute
{
protected override bool IsAuthorized(HttpActionContext actionContext)
{
var cnt = HttpContext.Current;
int id = Int32.Parse(cnt.Request.Params["id"]);
string apiKey = cnt.Request.Params["apiKey"];
string IP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
return true;
}
}
Run Code Online (Sandbox Code Playgroud)