I created a mvc4 webapi 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",
even though it is for mvc i am implementing it for web api. But isn't working
I created a html page for my client side. When the html page load it executes an ajax function which is intended to return "access Token".
I'm pretty sure the problem is that the Instance of OAuthServiceBase is null. I downloaded the code. It is initialized in the OauthAuthenticationModule. Have you remembered to include it in the webconfig?
Hey there kishore.eferns, I'm trying to do the exact same thing as you (create a mvc4 webapi project using 2-legged OAuth). Were you able to find a solution to your problems? Do you have a demo project that you could upload somewhere? Did you come across
any better more up to date tutorials?
kishore.efer...
Member
5 Points
22 Posts
Implementation of two legged OAuth 2.0 for WebApi
Sep 08, 2012 07:18 AM|LINK
I created a mvc4 webapi 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", even though it is for mvc i am implementing it for web api. But isn't working
I created a html page for my client side. When the html page load it executes an ajax function which is intended to return "access Token".
<script type="text/javascript"> $(document).ready(function () { var url = 'http://localhost:9792/api/Login'; $.get(url, function(data) { alert(data); }, "jsonp"); }); </script>The server side code is,
[NoCache] public class LoginController : ApiController { public LoginModelOAuth GetLogin() { var response = OAuthServiceBase.Instance.RequestToken(); LoginModelOAuth lmo = new LoginModelOAuth(); lmo.RequestToken = response.AccessToken; return lmo; } }RequestToken() method look 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 look like,
public class LoginModelOAuth { public string RequestToken { get; set; } public string ErrorMessage { get; set; } public string ReturnUrl { get; set; } }When i execute the client side code i receive following error
So i debugged server side code and in server side I got an error corresponding to this code
, and the error is
Will it work for web api?
If not, please suggest me any tutorial that would help.
Thanks.
jeppevkris
Member
2 Points
1 Post
Re: Implementation of two legged OAuth 2.0 for WebApi
Sep 26, 2012 08:44 PM|LINK
I'm pretty sure the problem is that the Instance of OAuthServiceBase is null. I downloaded the code. It is initialized in the OauthAuthenticationModule. Have you remembered to include it in the webconfig?
<httpModules> <add name="OAuthAuthentication" type="OAuth2.Mvc.Module.OAuthAuthenticationModule, OAuth2.Mvc, Version=1.0.0.0, Culture=neutral"/> </httpModules>jiggidy03
Member
2 Points
1 Post
Re: Implementation of two legged OAuth 2.0 for WebApi
Jan 15, 2013 03:35 AM|LINK
Hey there kishore.eferns, I'm trying to do the exact same thing as you (create a mvc4 webapi project using 2-legged OAuth). Were you able to find a solution to your problems? Do you have a demo project that you could upload somewhere? Did you come across any better more up to date tutorials?
Any help would be greatly appreciated!
Thanks.