我使用 VS2012RC 创建了一个 mvc4 webapi 项目。我试图在我的项目中实现两条腿的 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吗?
如果没有,请向我推荐任何有用的教程。
谢谢。