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);
            }
        }
示例#2
0
        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;
            }
        }