为获取书面MVC的Htt prequest扩展方法在网页API工作 [英] Getting HttpRequest extension method written for MVC to work in Web Api

查看:134
本文介绍了为获取书面MVC的Htt prequest扩展方法在网页API工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我与MVC,一个.NET库的oauth2的OAuth 2.0工作。我建立一个Web API项目,但是,我希望能得到这个库的Web API来工作。

I'm working with OAuth 2.0 for MVC, a .NET library for Oauth2. I'm building a Web Api project, however, and am hoping to get this library to work with Web Api.

我快到的问题是库使用上的Htt prequestBase两个扩展方法,它从控制器调用。

The problem I'm running into is that the library uses two extension methods on the HttpRequestBase that it calls from the controller.

下面是扩展方法:

public static string GetToken(this HttpRequest request)
    {
        var wrapper = new HttpRequestWrapper(request);
        return GetToken(wrapper);
    }

    public static string GetToken(this HttpRequestBase request)
    {
        if (request == null)
            return String.Empty;

        // Find Header
        var headerText = request.Headers[OAuthConstants.AuthorzationHeader];
        if (!String.IsNullOrEmpty(headerText))
        {
            var header = new AuthorizationHeader(headerText);
            if (string.Equals(header.Scheme, "OAuth", StringComparison.OrdinalIgnoreCase))
                return header.ParameterText.Trim();
        }

        // Find Clean Param
        var token = request.Params[OAuthConstants.AuthorzationParam];
        return !String.IsNullOrEmpty(token)
            ? token.Trim()
            : String.Empty;
    }

在MVC项目,他们只是从控制器调用Request.GetToken()。当然,网页API的请求是Htt的prequestMessage。恐怕解决pquest消息是超出了我的能力,现在的Htt prequest和Htt的$ P $之间的差异。

In the MVC project, they simply call Request.GetToken() from the controller. Of course, Web Api's request is an HttpRequestMessage. I'm afraid addressing the difference between HttpRequest and HttpRequest message is beyond my capabilities right now.

我可以转换成这种扩展方法与Htt的prequestMessage工作或以某种方式使其在网页API工作吗?

Can I convert this extension method to work with HttpRequestMessage or somehow make it work in Web Api??

谢谢!

推荐答案

您曾经有过的所有属性仍然可用(假定OAuthConstants.AuthorzationParam上查询字符串设定?)

All the properties you used to have are still available (assuming the OAuthConstants.AuthorzationParam is set on the query string?)

using System;
using System.Linq;
using System.Net.Http;

namespace YourApp
{
    public static class Extensions
    {
        public static string GetToken(this HttpRequestMessage request)
        {
           if (request == null)
               return String.Empty;

           // Find Header
           var headerText = request.Headers.GetValues(OAuthConstants.AuthorzationHeader).SingleOrDefault();
           if (!String.IsNullOrEmpty(headerText))
           {
               //Brevity...
           }

           // Find Clean Param
           var token = request.GetQueryNameValuePairs().SingleOrDefault(x => x.Key == OAuthConstants.AuthorzationParam).Value;
           return !String.IsNullOrEmpty(token)
               ? token.Trim()
               : String.Empty;
       }
   }

}

控制器

using System.Collections.Generic;
using System.Web.Http;
using YourApp;

namespace YourApp.Controllers
{
    public class FoosController : ApiController
    {
        public IEnumerable<string> Get()
        {
            var token = Request.GetToken();

            return null;
        }
    }
}

这篇关于为获取书面MVC的Htt prequest扩展方法在网页API工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆