如何使用webapi 2将数据从AuthorizeAttribute传递到Controller?

时间:2017-07-18 08:47:23

标签: c# asp.net-web-api

我创建了一个custom AuthorizeAttribute来验证某些OAuth credentials of user

一旦我获得了有效的用户,我想将响应数据返回给控制器如何在web api .net中实现这一点。

public class CustomAttribute : AuthorizeAttribute
{
    protected override bool IsAuthorized(HttpActionContext actionContext)
    {
        var response = mydata.Result.Content.ReadAsStringAsync();
        if (mydata.Result.StatusCode == HttpStatusCode.OK)
        {
            // return response data to controller
            return true;
        }
    }
}

我搜索了in mvc can be done like below

public class CustomAttribute : AuthorizeAttribute
{
   public string BlackListedUsers { get; set; }
   protected override bool AuthorizeCore(AuthorizationContext filterContext)
   {
     filterContext.HttpContext.Items["test"] = "foo";
     return true;
   }
}

在控制器中 -

_yourVariable = HttpContext.Items["test"];

如何在System.Web.Http的{​​{1}}中实现这一点,因为在webapi中我没有web api

4 个答案:

答案 0 :(得分:1)

这种方法可行,但不推荐。

在IsAuthorized函数中 -

protected override bool IsAuthorized(HttpActionContext actionContext)
{
    var response = mydata.Result.Content.ReadAsStringAsync();
    if (mydata.Result.StatusCode == HttpStatusCode.OK)
    {
        string someValue = "any value";
        actionContext.Request.Properties.Add(new KeyValuePair<string, object>("YourKeyName", someValue));
        return true;
    }
}

someValue可以是string,int或任何自定义对象。

在控制器中你可以像这样检索 -

object someObject;
Request.Properties.TryGetValue("YourKeyName", out someObject);

答案 1 :(得分:1)

在Web API 2.0中HttpActionContext.Request.Properties相当于AuthorizationContext .HttpContext.Items。因此,您可以向属性添加项目,并通过Request.Properties["keyName"]将其置于控制器中。

public class CustomAttribute : AuthorizeAttribute
{
    protected override bool IsAuthorized(HttpActionContext actionContext)
    {
        var response = mydata.Result.Content.ReadAsStringAsync();
        if (mydata.Result.StatusCode == HttpStatusCode.OK)
        {
            actionContext.Request.Properties["keyName"] = keyValue;
            return true;
        }
    }
}

答案 2 :(得分:0)

例如,如果您在该控制器上定义了具有属性MyProperty的MyController,那么在授权中您可能会有以下内容:

<div class="data">

        <div class="main">
            <div class="title">
                <div class="shownow">
                    <h4>Show It</h4>
                </div>
            </div>
            <div class="notshow">
                <ul class="catlist">
                    <li>Item 1</li>
                    <li>Item 2</li>
                    <li>Item 3</li>
                    <li>Item 4</li>
                </ul>
            </div>
        </div>

        <div class="main">
            <div class="title">
                <div class="shownow">
                    <h4>Show It</h4>
                </div>
            </div>
            <div class="notshow">
                <ul class="catlist">
                    <li>Item 1</li>
                    <li>Item 2</li>
                    <li>Item 3</li>
                    <li>Item 4</li>
                </ul>
            </div>
        </div>


        <div class="main">
            <div class="title">
                <div class="shownow">
                    <h4>Show It</h4>
                </div>
            </div>
            <div class="notshow">
                <ul class="catlist">
                    <li>Item 1</li>
                    <li>Item 2</li>
                    <li>Item 3</li>
                    <li>Item 4</li>
                </ul>
            </div>
        </div>

    </div>

并且在您的控制器中,您通常只能访问MyProperty

答案 3 :(得分:0)

我使用这样的代码

 protected override bool AuthorizeCore(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            BaseApiController Controller = actionContext.ControllerContext.Controller as BaseApiController;
            baseApi.Property = 10;
            return Controller.IsAuthorize();
        }
相关问题