示例#1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var context = new OAuthContextBuilder().FromHttpRequest(Request);

            IOAuthProvider provider = OAuthServicesLocator.Services.Provider;

            var tokenRepository = OAuthServicesLocator.Services.AccessTokenRepository;

            try
            {
                provider.AccessProtectedResourceRequest(context);

                var accessToken = tokenRepository.GetToken(context.Token);

                string userName = accessToken.UserName;

                var contactsDto = new {
                    Contacts = repository.GetContactsForUser(userName),
                    Title    = string.Format("Contacts for user \"" + userName + "\"")
                };

                Response.ContentType = "application/json";
                Response.Write(JsonConvert.SerializeObject(contactsDto));
                Response.End();
            }
            catch (OAuthException authEx)
            {
                // fairly naieve approach to status codes, generally you would want to examine eiter the inner exception of the
                // problem report to determine an appropriate status code for your technology / architecture.

                Response.StatusCode = 403;
                Response.Write(authEx.Report);
                Response.End();
            }
        }
        public override void ProcessRequest(ref RequestContext requestContext)
        {
            if (requestContext == null || requestContext.RequestMessage == null)
            {
                return;
            }

            Message request = requestContext.RequestMessage;

            var requestProperty = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];

            IOAuthContext context = new OAuthContextBuilder().FromUri(requestProperty.Method, request.Headers.To);

            try
            {
                _provider.AccessProtectedResourceRequest(context);

                AccessToken accessToken = _repository.GetToken(context.Token);

                TokenPrincipal principal = CreatePrincipalFromToken(accessToken);

                InitializeSecurityContext(request, principal);
            }
            catch (OAuthException authEx)
            {
                XElement response         = GetHtmlFormattedErrorReport(authEx);
                Message  reply            = Message.CreateMessage(MessageVersion.None, null, response);
                var      responseProperty = new HttpResponseMessageProperty {
                    StatusCode = HttpStatusCode.Forbidden, StatusDescription = authEx.Report.ToString()
                };
                responseProperty.Headers[HttpResponseHeader.ContentType] = "text/html";
                reply.Properties[HttpResponseMessageProperty.Name]       = responseProperty;
                requestContext.Reply(reply);

                requestContext = null;
            }
        }