コード例 #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();
            }
        }
コード例 #2
0
        public XDocument GetContactsForUser(string userName)
        {
            var repository = new ContactsRepository();

            return(new XDocument(
                       new XElement("contacts",
                                    new XAttribute("for", userName),
                                    repository.GetContactsForUser(userName)
                                    .Select(contact => new XElement("contact",
                                                                    new XAttribute("name", contact.FullName),
                                                                    new XAttribute("email", contact.Email))))));
        }
コード例 #3
0
        public Atom10FeedFormatter GetContacts()
        {
            var items = new List <SyndicationItem>();

            var repository = new ContactsRepository();

            foreach (Contact contact in repository.GetContactsForUser(Thread.CurrentPrincipal.Identity.Name))
            {
                items.Add(new SyndicationItem
                {
                    Id              = String.Format(CultureInfo.InvariantCulture, "http://contacts.org/{0}", Guid.NewGuid()),
                    Title           = new TextSyndicationContent(contact.FullName),
                    LastUpdatedTime = DateTime.UtcNow,
                    Authors         =
                    {
                        new SyndicationPerson
                        {
                            Name = "Sample Author"
                        }
                    },
                    Content = new TextSyndicationContent(contact.Email),
                });
            }

            // create the feed containing the syndication items.

            var feed = new SyndicationFeed
            {
                // The feed must have a unique stable URI id

                Id    = "http://contacts/Feed",
                Title = new TextSyndicationContent("Contacts feed"),
                Items = items
            };

            feed.AddSelfLink(WebOperationContext.Current.IncomingRequest.GetRequestUri());

            WebOperationContext.Current.OutgoingResponse.ContentType = ContentTypes.Atom;

            return(feed.GetAtom10Formatter());
        }