Esempio n. 1
0
        private void AuthenticateByChalkable()
        {
            RetrieveParams();

            var authUri = new Uri(Settings.Configuration.AuthUri);
            var acsUri = new Uri(Settings.Configuration.AcsUri);
            var redirectUri = new Uri(Settings.Configuration.RedirectUri);
            var appSecret = Settings.Configuration.AppSecret;
            var appName = Settings.Configuration.ApplicationName;
            var chlkRoot = Settings.Configuration.ChalkableRoot;

            if (Session[OAUTH_CLIENT] == null)
            {
                OauthClient = new SimpleOAuth2Client(authUri, acsUri, appName, appSecret, chlkRoot, redirectUri);
                string code = Request.Params["code"];
                string error = Request.Params["error"];
                if (!string.IsNullOrEmpty(error))
                {
                    throw new Exception(string.Format("OAuth error {0}. {1}", error, Request.Params["error_description"]));
                }
                if (!string.IsNullOrEmpty(code))
                {
                    OauthClient.Authorize(code);
                    Session[OAUTH_CLIENT] = OauthClient;
                }
                else
                {
                    var uri = OauthClient.BuildAuthorizationUri();
                    var callId = SaveParamsToCache();
                    Response.Redirect(uri + "&" + Settings.CALL_ID_PARAM + "=" + callId, true);
                }
            }
            else
                OauthClient = Session[OAUTH_CLIENT] as SimpleOAuth2Client;
        }
Esempio n. 2
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();
        }