HttpClient发布到授权角色归因操作

时间:2018-07-11 06:41:28

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

如何发布/获取归因于

的mvc操作

[Authorize(Roles = "Admin,Supervisor")]吗?

这是我的动作

[AcceptVerbs("Get", "Post")]
[Authorize(Roles = "Operator,Supervisor")]
public string SortManageStocks(string sorter = "StockId", bool isAscending = false, string searchString = "", int currentPage = 1, int itemsPerPage = 50, bool showAll = false)
{
    StockViewModel viewModel = new StockViewModel();
    try
    {
    ...............

我通过mvc之类的方式调用它时运行良好:

string result = SortManageStocks(sorter, isAscending, searchString, currentPage, itemsPerPage, showAll);

如何使用httpclient方法发布/执行此操作?

这是我尝试过的:

try
{
    using (var client = new HttpClient())
    {
        var values = new List<KeyValuePair<string, string>>();

        // add values to data for post
        values.Add(new KeyValuePair<string, string>("sorter", "StockId"));
        values.Add(new KeyValuePair<string, string>("isAscending", "false"));
        values.Add(new KeyValuePair<string, string>("searchString", ""));
        values.Add(new KeyValuePair<string, string>("currentPage", "1"));
        values.Add(new KeyValuePair<string, string>("itemsPerPage", "50"));
        values.Add(new KeyValuePair<string, string>("showAll", "true"));

        FormUrlEncodedContent content = new FormUrlEncodedContent(values);

        var result = client.PostAsync("http://localhost:50904/Operator/SortManageStocks", content).Result;
        var response = result.Content.ReadAsStringAsync().Result;

        if (result.StatusCode == HttpStatusCode.Unauthorized)
        {
            var z = "Not authorized";
        }
    }
}

如何通过httpclient传递authorize(roles =“”)属性?

0 个答案:

没有答案