4

VS2012RC を使用して mvc4 webapi プロジェクトを作成しました。プロジェクトに 2 本足の Oauth 2 を実装しようとしました。チュートリアル「http://community.codesmithtools.com/CodeSmith_Community/b/tdupont/archive/2011/03/18/oauth-2-0-for-mvc-two-legged-implementation.aspx」に従いましたが、それはmvc用です。Web API用に実装しています。しかし、機能していません

クライアント側の html ページを作成しました。HTMLページがロードされると、「アクセストークン」を返すことを目的としたajax関数が実行されます。

       <script type="text/javascript">
        $(document).ready(function () {
        var url = 'http://localhost:9792/api/Login';
            $.get(url, function(data) {

            alert(data);
            }, "jsonp");
        });
        </script>

サーバー側のコードは、

[NoCache]
public class LoginController : ApiController
{

    public LoginModelOAuth GetLogin()
    {

        var response = OAuthServiceBase.Instance.RequestToken();

        LoginModelOAuth lmo = new LoginModelOAuth();
         lmo.RequestToken = response.RequestToken;

        return lmo;
    }

}

RequestToken()メソッドは次のようになります。

    public override OAuthResponse RequestToken()
    {
        var token = Guid.NewGuid().ToString("N");
        var expire = DateTime.Now.AddMinutes(5);
        RequestTokens.Add(token, expire);

        return new OAuthResponse
        {
            Expires = (int)expire.Subtract(DateTime.Now).TotalSeconds,
            RequestToken = token,
            RequireSsl = false,
            Success = true
        };
    }

LoginModelOAuthモデルは次のようになります。

public class LoginModelOAuth
{

    public string RequestToken { get; set; }      


    public string ErrorMessage { get; set; }

    public string ReturnUrl { get; set; }
}

クライアント側のコードを実行すると、次のエラーが表示されます

"500 Internal Server Error"

だから私はサーバー側のコードをデバッグし、サーバー側でこのコードに対応するエラーが発生しました

"var response = OAuthServiceBase.Instance.RequestToken();" 、エラーは

  NullReferenceException was unhandled by user code

  "Object reference not set to an instance of an object."

私のwebconfigファイルは次のようになります

  <configuration>
  <configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="oauth" type="OAuth2.Mvc.Configuration.OAuthSection, OAuth2.Mvc, Version=1.0.0.0, Culture=neutral"/>
<sectionGroup name="dotNetOpenAuth" type="DotNetOpenAuth.Configuration.DotNetOpenAuthSection, DotNetOpenAuth.Core">
  <section name="messaging" type="DotNetOpenAuth.Configuration.MessagingElement, DotNetOpenAuth.Core" requirePermission="false" allowLocation="true" />
  <section name="reporting" type="DotNetOpenAuth.Configuration.ReportingElement, DotNetOpenAuth.Core" requirePermission="false" allowLocation="true" />
</sectionGroup>
 </configSections>
   <oauth defaultProvider="DemoProvider" defaultService="DemoService">
    <providers>
      <add name="DemoProvider" type="MillionNodesApi.OAuth.DemoProvider, MillionNodesApi" />
    </providers>
<services>
  <add name="DemoService" type="MillionNodesApi.OAuth.DemoService, MillionNodesApi" />
</services>
  </oauth>
    <system.web>
    <httpModules>
      <add name="OAuthAuthentication"     type="OAuth2.Mvc.Module.OAuthAuthenticationModule, OAuth2.Mvc, Version=1.0.0.0, Culture=neutral"/>
    </httpModules>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
     <pages>
     <namespaces>
    <add namespace="System.Web.Helpers" />
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Optimization" />
    <add namespace="System.Web.Routing" />
    <add namespace="System.Web.WebPages" />
  </namespaces>
</pages>
</system.web>
 <dotNetOpenAuth>
    <messaging>
      <untrustedWebRequest>
    <whitelistHosts>
      <!-- Uncomment to enable communication with localhost (should generally not activate in production!) -->
      <!--<add name="localhost" />-->
    </whitelistHosts>
  </untrustedWebRequest>
</messaging>
<!-- Allow DotNetOpenAuth to publish usage statistics to library authors to improve the library. -->
<reporting enabled="true" />

Web APIで動作しますか?

そうでない場合は、役立つチュートリアルを提案してください。

ありがとう。

4

1 に答える 1

2

問題は、統合モードで IIS7 を使用していることだと思うので、構成を system.web/httpModules から system.webServer/modulesに移行する必要があります。

<system.web>
    <httpModules>
       <add name="OAuthAuthentication"     type="OAuth2.Mvc.Module.OAuthAuthenticationModule, OAuth2.Mvc, Version=1.0.0.0, Culture=neutral"/>
    </httpModules>

なる

<system.webServer>
    <modules>
        <add name="OAuthAuthentication"     type="OAuth2.Mvc.Module.OAuthAuthenticationModule, OAuth2.Mvc, Version=1.0.0.0, Culture=neutral" preCondition="" />
    </modules>

こちらもお試しください

これを設定することで、モジュールをヒットさせることができます

<modules runAllManagedModulesForAllRequests="true">

多分

それでも問題が解決しない場合は、モジュールの実行順序が原因である可能性があります。ルーティングのものを削除し、OAuth のものを一番上に配置してみてください...

<modules>
  <remove name="UrlRoutingModule-4.0" />
<add name="OAuthAuthentication"     type="OAuth2.Mvc.Module.OAuthAuthenticationModule, OAuth2.Mvc, Version=1.0.0.0, Culture=neutral" preCondition="" />
  <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />

于 2012-09-11T08:58:19.650 に答える