小编dev*_*en1的帖子

如何在 ASP.NET 中的自定义授权属性中获取引用数据

如何在 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)

c# asp.net custom-attributes

5
推荐指数
0
解决办法
747
查看次数

标签 统计

asp.net ×1

c# ×1

custom-attributes ×1