public ActionResult ViewResourceOwnerData() { IResourceContext context = new ResourceContextBuilder().FromHttpRequest(ControllerContext.HttpContext.Request); IResourceProvider provider = ServiceLocator.Current.GetInstance <IResourceProvider>(); object[] list = new object[] { new { FirstName = "Geoff", LastName = "Horsey" }, new { FirstName = "John", LastName = "Doe" }, new { FirstName = "Jane", LastNmae = "Doe" } }; try { provider.AccessProtectedResource(context); provider.ValidateScope(context, new string[] { "view" }); return(Json(list, JsonRequestBehavior.AllowGet)); } catch (OAuthErrorResponseException <IResourceProvider> x) { throw new HttpException(x.HttpStatusCode, x.Message); } }
public void TestCreatingContextFromUrl() { ResourceContextBuilder builder = new ResourceContextBuilder(); IResourceContext context = builder.FromUri("http://wwww.mysite.com?bearer_token=token"); Assert.AreEqual("token", context.QueryString["bearer_token"]); Assert.IsNull(context.Form); Assert.IsNull(context.Headers); }
public void TestCreatingContextFromUrl() { ResourceContextBuilder builder = new ResourceContextBuilder(); IResourceContext context = builder.FromUri("http://wwww.mysite.com?bearer_token=token"); Assert.AreEqual("token", context.QueryString["bearer_token"]); Assert.IsNull(context.Form); Assert.IsNull(context.Headers); }
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { try { var uri = request.RequestUri; IResourceProvider provider = ServiceLocator.Current.GetInstance<IResourceProvider>(); IResourceContext context = new ResourceContextBuilder().FromHttpRequest(new HttpRequestWrapper(HttpContext.Current.Request)); try { provider.AccessProtectedResource(context); TokenPrincipal principal = new TokenPrincipal(new GenericIdentity(context.Token.Token, "OAuth"), context.Token.Scope, context.Token); Thread.CurrentPrincipal = principal; //HttpContext.Current.User = principal; } catch (OAuthErrorResponseException<IResourceContext> x) { Log.Info(m => m("Failed to authorize the token. Error: {0}; Message: {1}", x.Error, x.Message), x); } catch (OAuthFatalException x) { StringBuilder report = new StringBuilder(); FormatReportHeader(report, "QueryString"); FormatReportDictionary(report, context.QueryString); FormatReportHeader(report, "Form"); FormatReportDictionary(report, context.Form); FormatReportHeader(report, "Headers"); FormatReportDictionary(report, context.Headers); FormatReportHeader(report, "URL"); report.AppendLine(HttpContext.Current.Request.Url.ToString()); Log.Info(m => m("{0}\r\n{1}", x.Message, report.ToString()), x); } } catch (Exception x) { Log.Error(x.Message, x); throw; } return base.SendAsync(request, cancellationToken); }
public void TestCreatingContextFromRequest() { NameValueCollection form = new NameValueCollection(); NameValueCollection query = new NameValueCollection(); NameValueCollection headers = new NameValueCollection(); Mock<HttpRequestBase> mckRequest = new Mock<HttpRequestBase>(); mckRequest.SetupGet(x => x.Form).Returns(form); mckRequest.SetupGet(x => x.QueryString).Returns(query); mckRequest.SetupGet(x => x.Headers).Returns(headers); ResourceContextBuilder builder = new ResourceContextBuilder(); IResourceContext context = builder.FromHttpRequest(mckRequest.Object); Assert.AreEqual(form, context.Form); Assert.AreEqual(query, context.QueryString); Assert.AreEqual(headers, context.Headers); }
public void TestCreatingContextFromRequest() { NameValueCollection form = new NameValueCollection(); NameValueCollection query = new NameValueCollection(); NameValueCollection headers = new NameValueCollection(); Mock <HttpRequestBase> mckRequest = new Mock <HttpRequestBase>(); mckRequest.SetupGet(x => x.Form).Returns(form); mckRequest.SetupGet(x => x.QueryString).Returns(query); mckRequest.SetupGet(x => x.Headers).Returns(headers); ResourceContextBuilder builder = new ResourceContextBuilder(); IResourceContext context = builder.FromHttpRequest(mckRequest.Object); Assert.AreEqual(form, context.Form); Assert.AreEqual(query, context.QueryString); Assert.AreEqual(headers, context.Headers); }
protected void Application_AuthenticateRequest(object sender, EventArgs e) { try { IResourceProvider provider = ServiceLocator.Current.GetInstance <IResourceProvider>(); IResourceContext context = new ResourceContextBuilder().FromHttpRequest(new HttpRequestWrapper(HttpContext.Current.Request)); try { provider.AccessProtectedResource(context); TokenPrincipal principal = new TokenPrincipal(new GenericIdentity(context.Token.Token, "OAuth"), context.Token.Scope, context.Token); HttpContext.Current.User = principal; } catch (OAuthErrorResponseException <IResourceContext> x) { Log.Info(m => m("Failed to authorize the token. Error: {0}; Message: {1}", x.Error, x.Message), x); return; } catch (OAuthFatalException x) { StringBuilder report = new StringBuilder(); FormatReportHeader(report, "QueryString"); FormatReportDictionary(report, context.QueryString); FormatReportHeader(report, "Form"); FormatReportDictionary(report, context.Form); FormatReportHeader(report, "Headers"); FormatReportDictionary(report, context.Headers); FormatReportHeader(report, "URL"); report.AppendLine(HttpContext.Current.Request.Url.ToString()); Log.Info(m => m("{0}\r\n{1}", x.Message, report.ToString()), x); } } catch (Exception x) { Log.Error(x.Message, x); throw; } }
protected void Application_AuthenticateRequest(object sender, EventArgs e) { try { IResourceProvider provider = ServiceLocator.Current.GetInstance<IResourceProvider>(); IResourceContext context = new ResourceContextBuilder().FromHttpRequest(new HttpRequestWrapper(HttpContext.Current.Request)); try { provider.AccessProtectedResource(context); TokenPrincipal principal = new TokenPrincipal(new GenericIdentity(context.Token.Token, "OAuth"), context.Token.Scope, context.Token); HttpContext.Current.User = principal; } catch (OAuthErrorResponseException<IResourceContext> x) { Log.Info(m => m("Failed to authorize the token. Error: {0}; Message: {1}", x.Error, x.Message), x); return; } catch (OAuthFatalException x) { StringBuilder report = new StringBuilder(); FormatReportHeader(report, "QueryString"); FormatReportDictionary(report, context.QueryString); FormatReportHeader(report, "Form"); FormatReportDictionary(report, context.Form); FormatReportHeader(report, "Headers"); FormatReportDictionary(report, context.Headers); FormatReportHeader(report, "URL"); report.AppendLine(HttpContext.Current.Request.Url.ToString()); Log.Info(m => m("{0}\r\n{1}", x.Message, report.ToString()), x); } } catch (Exception x) { Log.Error(x.Message, x); throw; } }
public ActionResult ViewResourceOwnerData() { IResourceContext context = new ResourceContextBuilder().FromHttpRequest(ControllerContext.HttpContext.Request); IResourceProvider provider = ServiceLocator.Current.GetInstance<IResourceProvider>(); object[] list = new object[] { new { FirstName = "Geoff", LastName="Horsey"}, new { FirstName = "John", LastName = "Doe"}, new { FirstName = "Jane", LastNmae ="Doe"} }; try { provider.AccessProtectedResource(context); provider.ValidateScope(context, new string[] { "view" }); return Json(list, JsonRequestBehavior.AllowGet); } catch(OAuthErrorResponseException<IResourceProvider> x) { throw new HttpException(x.HttpStatusCode, x.Message); } }