Beispiel #1
0
        public ActionResult Connect(string code, string error)
        {
            if (error == "usercancelled")
            {
                return(RedirectToAction("Index"));
            }


            string clientId     = "06B059BE-E8AF-4FD5-A784-833A988A64A0";
            string clientSecret = "AE3A7E6E-DA37-4F99-96A9-70FFCCAACDE1";
            string redirectUri  = "http://localhost:3476/Home/Connect/";



            string authorizeUri = "http://brewbuddy.azurewebsites.net/authorize";
            string scope        = "http://www.brewbuddy.net/";

            var client = new SimpleOAuth2Client(
                new Uri(authorizeUri),
                new Uri("https://brewbuddy-euwest-1-sb.accesscontrol.windows.net/v2/OAuth2-13/"),
                clientId,
                clientSecret,
                scope,
                new Uri(redirectUri));

            if (string.IsNullOrWhiteSpace(code) && string.IsNullOrWhiteSpace(error))
            {
                return(Redirect(client.BuildAuthorizationUri().ToString()));
            }

            client.Authorize(code);

            HttpWebRequest webRequest = HttpWebRequest.Create(new Uri("http://brewbuddy.azurewebsites.net/api/v1/recipes")) as HttpWebRequest;

            webRequest.Method        = WebRequestMethods.Http.Get;
            webRequest.ContentLength = 0;
            client.AppendAccessTokenTo(webRequest);

            var responseText = "";

            try
            {
                var response = webRequest.GetResponse();
                responseText = new StreamReader(response.GetResponseStream()).ReadToEnd();

                var recipes = JsonConvert.DeserializeObject <List <RecipeViewModel> >(responseText);

                return(View("Recipes", recipes));
            }
            catch (WebException wex)
            {
                responseText = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
            }

            return(Content(responseText));
        }
        public ActionResult Index(string code, string error)
        {
            string authorizeUri = "http://localhost:31875/authorize";
            string clientId     = "testclient3";
            string clientSecret = "testsecret";
            string redirectUri  = "http://localhost:31875/";
            string scope        = "http://localhost:31875/";

            // Register the app (this should be done elsewhere!)
            try
            {
                var x = new ApplicationRegistrationService();
                x.RegisterApplication(clientId, clientSecret, redirectUri, clientId);
            }
            catch
            {
            }

            var client = new SimpleOAuth2Client(
                new Uri(authorizeUri),
                new Uri("https://brewbuddy-prod.accesscontrol.windows.net/v2/OAuth2-13/"),
                clientId,
                clientSecret,
                scope,
                new Uri(redirectUri));

            if (string.IsNullOrWhiteSpace(code) && string.IsNullOrWhiteSpace(error))
            {
                return(Redirect(client.BuildAuthorizationUri().ToString()));
            }

            client.Authorize(code);

            HttpWebRequest webRequest = HttpWebRequest.Create(new Uri("http://localhost:31875/api/v1/Sample")) as HttpWebRequest;

            webRequest.Method        = WebRequestMethods.Http.Get;
            webRequest.ContentLength = 0;
            client.AppendAccessTokenTo(webRequest);

            var responseText = "";

            try
            {
                var response = webRequest.GetResponse();
                responseText = new StreamReader(response.GetResponseStream()).ReadToEnd();
            }
            catch (WebException wex)
            {
                responseText = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
            }

            return(Content(responseText));
        }
        //
        // GET: /Home/

        public ActionResult Index()
        {
            var clientId          = "yourclientid";
            var clientSecret      = "yoursecret";
            var redirectUri       = "http://yourredirecturi/";
            var scope             = "http://yourscope/";
            var authServer        = "http://localhost/authorize";
            var apiRoot           = "http://localhost/yourapi/";
            var accessTokenServer = "https://youracsnamespace-prod.accesscontrol.windows.net/v2/OAuth2-13/";

            var client = new SimpleOAuth2Client(new Uri(authServer), new Uri(accessTokenServer), clientId, clientSecret, scope, new Uri(redirectUri), ClientMode.TwoLegged);

            client.Authorize();

            var req = WebRequest.CreateHttp(apiRoot);

            client.AppendAccessTokenTo(req);

            var response = req.GetResponse();

            return(View());
        }