예제 #1
0
        public ActionResult Index()
        {
            #region Get Authorization code
            var query   = Request.Query;
            var referer = Request.Headers["Referer"].ToString();
            var code    = string.Empty;
            if (query.ContainsKey("code"))
            {
                code = query["code"];                                       //if (query.ContainsKey("session_state")) state = query["session_state"];
            }
            #endregion
            ViewBag.IsSignin = !string.IsNullOrEmpty(code);
            var brandsList = new List <KeyValuePair <string, bool> >();
            var brands     = Environment.GetEnvironmentVariable("Brands");
            if (string.IsNullOrEmpty(brands))
            {
                var pair = new KeyValuePair <string, bool>();
                foreach (var i in Environment.GetEnvironmentVariables())
                {
                    if (i.GetType() == typeof(DictionaryEntry))
                    {
                        var key = string.Empty;
                        if (((DictionaryEntry)i).Key.ToString().Contains("MicrosoftIdentity"))
                        {
                            key = "microsoft";
                        }
                        if (((DictionaryEntry)i).Key.ToString().Contains("FacebookIdentity"))
                        {
                            key = "facebook";
                        }
                        if (((DictionaryEntry)i).Key.ToString().Contains("TwitterIdentity"))
                        {
                            key = "twitter";
                        }
                        if (!string.IsNullOrEmpty(key))
                        {
                            pair = new KeyValuePair <string, bool>(key, false);
                            if (brandsList.Where(d => d.Key == key).Count() == 0)
                            {
                                brandsList.Add(pair);
                            }
                        }
                    }
                }
                brands = JsonSerializer.Serialize(brandsList);
            }
            brandsList = JsonSerializer.Deserialize <List <KeyValuePair <string, bool> > >(brands);
            if (ViewBag.IsSignin)
            {
                using (var httpClient = new HttpClient())
                {
                    #region Get Access token
                    var properties = "client_id=" + clientId + "&client_secret=" + clientSecret;
                    properties += "&redirect_uri=" + redirectUri;
                    properties += "&scope=" + profileScope;
                    properties += "&code=" + code + "&grant_type=authorization_code";
                    var seed = Environment.GetEnvironmentVariable("ChallengeSeed");
                    //challenge length is 43-128 characters
                    var challengeLength = int.Parse(Environment.GetEnvironmentVariable("ChallengeLength"));
                    properties += "&code_verifier=" + new Encryptor().CreateChallengeText(seed, challengeLength);
                    HttpResponseMessage res = null;
                    var brand = Environment.GetEnvironmentVariable("TargetBrand");
                    if (brand == "microsoft")
                    {
                        var content = new StringContent(properties, Encoding.UTF8, "application/x-www-form-urlencoded");
                        res = httpClient.PostAsync(tokenSTS, content).Result;
                    }
                    else if (Environment.GetEnvironmentVariable("TargetBrand") == "facebook")
                    {
                        res = httpClient.GetAsync(tokenSTS + "?" + properties).Result;
                    }
                    string resultJson   = res.Content.ReadAsStringAsync().Result;
                    var    accessResult = JsonSerializer.Deserialize <OAuthTokenModel>(resultJson);
                    var    tempElement  = new JsonElement();
                    #endregion
                    var token = string.Empty;
                    if (res.IsSuccessStatusCode)
                    {
                        var doc = JsonDocument.Parse(resultJson).RootElement;
                        if (doc.TryGetProperty("access_token", out tempElement))
                        {
                            token = tempElement.GetString();
                        }

                        using (var httpClient2 = new HttpClient())
                        {
                            httpClient2.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
                            var    res2        = httpClient2.GetAsync(profileResource).Result;
                            string resultJson2 = res2.Content.ReadAsStringAsync().Result;
                            if (res2.IsSuccessStatusCode)
                            {
                                BrandPrimitive user = null;
                                if (brand == "microsoft")
                                {
                                    user = JsonSerializer.Deserialize <MSGraphUser>(resultJson2);
                                    ViewBag.AccountName = (user as MSGraphUser).displayName;
                                    var appUser = new ApplicationUser <MSGraphUser>()
                                    {
                                        UserCore = (user as MSGraphUser), BrandName = IPBlandType.Microsoft, AccessList = new List <AccessHistory>()
                                    };
                                    appUser.AccessList.Add(new AccessHistory()
                                    {
                                        AuthTokens = accessResult, AADEndPoint = tokenSTS, AuthCode = code, ClientId = clientId, GrantType = "authorization_code", Redirect = new Uri(redirectUri), Scope = profileScope
                                    });
                                    var loginState = new LoginModel <MSGraphUser>()
                                    {
                                        AuthrizeUrl = tokenSTS, IsLogin = true, User = appUser
                                    };
                                    new DataAccessLayer().SetStateManagement <LoginModel <MSGraphUser> >(loginState);
                                }
                                else
                                {
                                    user = JsonSerializer.Deserialize <FBGraphUser>(resultJson2);
                                    ViewBag.AccountName = (user as FBGraphUser).name;
                                    var appUser = new ApplicationUser <FBGraphUser>()
                                    {
                                        UserCore = (user as FBGraphUser), BrandName = IPBlandType.Facebook
                                    };
                                    var loginState = new LoginModel <FBGraphUser>()
                                    {
                                        AuthrizeUrl = tokenSTS, IsLogin = true, User = appUser
                                    };
                                    new DataAccessLayer().SetStateManagement <LoginModel <FBGraphUser> >(loginState);
                                }
                                var target = brandsList.Where(b => b.Key == brand).FirstOrDefault();
                                brandsList.Remove(target);
                                brandsList.Add(new KeyValuePair <string, bool>(brand, true));
                            }
                        }
                    }
                }
            }
            ViewBag.Brands   = brandsList;
            ViewBag.AppTitle = "Screen Reservation";
            return(View());
        }
예제 #2
0
 public ActionResult Index()
 {
     #region Get Authorization code
     var query   = Request.Query;
     var referer = Request.Headers["Referer"].ToString();
     var code    = string.Empty;
     if (query.ContainsKey("code"))
     {
         code = query["code"];                                       //if (query.ContainsKey("session_state")) state = query["session_state"];
     }
     #endregion
     ViewBag.IsSignin = !string.IsNullOrEmpty(code);
     if (ViewBag.IsSignin)
     {
         using (var httpClient = new HttpClient())
         {
             #region Get Access token
             var properties = "client_id=" + clientId + "&client_secret=" + clientSecret;
             properties += "&redirect_uri=" + redirectUri;
             properties += "&scope=" + profileScope;
             properties += "&code=" + code + "&grant_type=authorization_code";
             HttpResponseMessage res = null;
             ViewBag.Brand = Environment.GetEnvironmentVariable("TargetBrand");
             if (ViewBag.Brand == "microsoft")
             {
                 var content = new StringContent(properties, Encoding.UTF8, "application/x-www-form-urlencoded");
                 res = httpClient.PostAsync(tokenSST, content).Result;
             }
             else if (Environment.GetEnvironmentVariable("TargetBrand") == "facebook")
             {
                 res = httpClient.GetAsync(tokenSST + "?" + properties).Result;
             }
             string resultJson  = res.Content.ReadAsStringAsync().Result;
             var    tempElement = new JsonElement();
             #endregion
             var token = string.Empty;
             if (res.IsSuccessStatusCode)
             {
                 var doc = JsonDocument.Parse(resultJson).RootElement;
                 if (doc.TryGetProperty("access_token", out tempElement))
                 {
                     token = tempElement.GetString();
                 }
                 using (var httpClient2 = new HttpClient())
                 {
                     httpClient2.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
                     var    res2        = httpClient2.GetAsync(profileResource).Result;
                     string resultJson2 = res2.Content.ReadAsStringAsync().Result;
                     if (res2.IsSuccessStatusCode)
                     {
                         BrandPrimitive user = null;
                         if (ViewBag.Brand == "microsoft")
                         {
                             user = JsonSerializer.Deserialize <MSGraphUser>(resultJson2);
                             ViewBag.AccountName = (user as MSGraphUser).displayName;
                             var appUser = new ApplicationUser <MSGraphUser>()
                             {
                                 UserCore = (user as MSGraphUser), BrandName = IPBlandType.Microsoft
                             };
                             var loginState = new LoginModel <MSGraphUser>()
                             {
                                 AuthrizeUrl = tokenSST, IsLogin = true, User = appUser
                             };
                             new DataAccessLayer().SetStateManagement <LoginModel <MSGraphUser> >(loginState);
                         }
                         else
                         {
                             user = JsonSerializer.Deserialize <FBGraphUser>(resultJson2);
                             ViewBag.AccountName = (user as FBGraphUser).name;
                             var appUser = new ApplicationUser <FBGraphUser>()
                             {
                                 UserCore = (user as FBGraphUser), BrandName = IPBlandType.Facebook
                             };
                             var loginState = new LoginModel <FBGraphUser>()
                             {
                                 AuthrizeUrl = tokenSST, IsLogin = true, User = appUser
                             };
                             new DataAccessLayer().SetStateManagement <LoginModel <FBGraphUser> >(loginState);
                         }
                     }
                     else
                     {
                         ViewBag.Brand = string.Empty;
                     }
                 }
             }
         }
     }
     ViewBag.AppTitle = "Screen Reservation";
     return(View());
     //return View(new IndexViewModel
     //{
     //	Comments = _comments.Take(COMMENTS_PER_PAGE).ToList().AsReadOnly(),
     //	CommentsPerPage = COMMENTS_PER_PAGE,
     //	Page = 1
     //});
 }