コード例 #1
0
 public ActionResult Index()
 {
     var userAuthStateId = Settings.GetUserAuthStateId(ControllerContext.HttpContext);
     //todo: add support for Google and other auth providers
     if (Data.GetUserSessionToken(userAuthStateId, Settings.AzureADAuthority) != null)
     {
         return RedirectToAction("Index", "Message");
     }
     ViewBag.StateKey = userAuthStateId;
     var tk = new SessionToken();
     return View(tk);
 }
        public static async Task<DropBoxSpaceUsage> GetDropBoxSpaceUsage(SessionToken token)
        {
            var url = "https://api.dropboxapi.com/2/users/get_space_usage";
            DropBoxSpaceUsage usage = null;
            using (var client = new HttpClient())
            {
                using (var request = new HttpRequestMessage(HttpMethod.Post, url))
                {
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);

                    using (var response = await client.SendAsync(request))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            usage =
                                JsonConvert.DeserializeObject<DropBoxSpaceUsage>(
                                    await response.Content.ReadAsStringAsync());
                        }
                    }
                }
            }
            return usage;
        }
        private static async Task SaveAuthToken(AuthState authState, OAuthResult authResult, FacebookUserProfile userProfile)
        {
            using (var db = new AddInContext())
            {
                var existingToken =
                    await
                        db.SessionTokens.FirstOrDefaultAsync(
                            t => t.Provider == Settings.FacebookAuthority && t.Id == authState.stateKey);
                if (existingToken != null)
                {
                    db.SessionTokens.Remove(existingToken);
                }

                var token = new SessionToken()
                {
                    Id = authState.stateKey,
                    CreatedOn = DateTime.Now,
                    AccessToken = authResult.access_token,
                    Provider = Settings.FacebookAuthority,
                    Username = userProfile.id,
                };
                db.SessionTokens.Add(token);
                await db.SaveChangesAsync();
            }
        }
        private static async Task SaveAuthToken(AuthState authState, OAuthResult authResult)
        {
            var idToken = SessionToken.ParseJwtToken(authResult.id_token);
            string username = null;
            var userNameClaim = idToken.Claims.FirstOrDefault(x => x.Type == "upn" || x.Type == "preferred_username");
            if (userNameClaim != null)
                username = userNameClaim.Value;

            using (var db = new AddInContext())
            {
                var existingToken =
                    await
                        db.SessionTokens.FirstOrDefaultAsync(
                            t => t.Provider == Settings.AzureAD2Authority && t.Id == authState.stateKey);
                if (existingToken != null)
                {
                    db.SessionTokens.Remove(existingToken);
                }

                var token = new SessionToken()
                {
                    Id = authState.stateKey,
                    CreatedOn = DateTime.Now,
                    AccessToken = authResult.access_token,
                    Provider = Settings.AzureAD2Authority,
                    Username = username,
                };
                db.SessionTokens.Add(token);
                await db.SaveChangesAsync();
            }
        }
        private static async Task SaveAuthToken(AuthState authState, OAuthResult authResult)
        {
            using (var db = new AddInContext())
            {
                var existingToken =
                    await
                        db.SessionTokens.FirstOrDefaultAsync(
                            t => t.Provider == Settings.GoogleAuthority && t.Id == authState.stateKey);
                if (existingToken != null)
                {
                    db.SessionTokens.Remove(existingToken);
                }
                string username = null;
                var jwt = SessionToken.ParseJwtToken(authResult.id_token);
                var emailClaim = jwt.Claims.FirstOrDefault(c => c.Type == "email");
                if (emailClaim != null)
                    username = emailClaim.Value;

                var token = new SessionToken()
                {
                    Id = authState.stateKey,
                    CreatedOn = DateTime.Now,
                    AccessToken = authResult.access_token,
                    Provider = Settings.GoogleAuthority,
                    Username = username,
                };
                db.SessionTokens.Add(token);
                await db.SaveChangesAsync();
            }
        }