I created a webvi mvc4 project using VS2012RC. I tried to implement two-legged Oauth 2 in my project. I followed the tutorial "http://community.codesmithtools.com/CodeSmith_Community/b/tdupont/archive/2011/03/18/oauth-2-0-for-mvc-two-legged-implementation.aspx", although this for mvc, I implement it for web api. But does not work
I created an html page for my client side. When loading an html page performs an ajax function, which is designed to return an "access token."
<script type="text/javascript"> $(document).ready(function () { var url = 'http://localhost:9792/api/Login'; $.get(url, function(data) { alert(data); }, "jsonp"); }); </script>
Server Side Code:
[NoCache] public class LoginController : ApiController { public LoginModelOAuth GetLogin() { var response = OAuthServiceBase.Instance.RequestToken(); LoginModelOAuth lmo = new LoginModelOAuth(); lmo.RequestToken = response.RequestToken; return lmo; }
}
The RequestToken () method looks like
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 Model ,
public class LoginModelOAuth { public string RequestToken { get; set; } public string ErrorMessage { get; set; } public string ReturnUrl { get; set; } }
When I execute the code on the client side, I get the following error
"500 Internal Server Error"
So, I debugged the code on the server side and on the server side I got an error corresponding to this code
"var response = OAuthServiceBase.Instance.RequestToken ();", and error
NullReferenceException was unhandled by user code "Object reference not set to an instance of an object."
My webconfig file looks like
<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> </whitelistHosts> </untrustedWebRequest> </messaging> <reporting enabled="true" />
Will it work for web api?
If not, offer me any tutorial that will help.
Thanks.