private static void UpdateUserAccess(User user, string projectName, string applicationName, string cluster, Access access) { user.Access.AddRule(new AccessRules { Project = projectName, Application = applicationName, Cluster = cluster, Access = access }); var hostName = ApplicationConfiguration.Get("pandora_api_url"); var url = hostName + "/api/Users/" + ClaimsPrincipal.Current.Id(); var restClient = new RestSharp.RestClient(url); var editRequest = new RestSharp.RestRequest(); editRequest.Method = RestSharp.Method.PUT; editRequest.RequestFormat = RestSharp.DataFormat.Json; editRequest.AddHeader("Content-Type", "application/json;charset=utf-8"); editRequest.AddHeader("Authorization", "Bearer " + ClaimsPrincipal.Current.IdToken()); editRequest.AddBody(user); var editResult = restClient.Execute(editRequest); }
private void GetUserInfo(User user) { var url = "https://www.googleapis.com/plus/v1/people/" + user.Id; var restClient = new RestSharp.RestClient(url); var request = new RestSharp.RestRequest(); request.Method = RestSharp.Method.GET; request.AddHeader("Authorization", "Bearer " + User.AccessToken()); var result = restClient.Execute<GoogleUserInfo>(request); var info = result.Data; if (info != null) { user.AvatarUrl = info.Image.Url; user.FullName = info.DisplayName; user.FirstName = info.Name.GivenName; user.LastName = info.Name.FamilyName; if (info.Organizations != null) { var organization = info.Organizations.FirstOrDefault(x => x.Primay == true); if (organization == null) organization = info.Organizations.FirstOrDefault(); if (organization != null) user.Organization = organization.Name; } } }
private void CreateUser(User user, string token) { var hostName = ApplicationConfiguration.Get("pandora_api_url"); var url = hostName + "/api/Users/" + user.Id; var restClient = new RestSharp.RestClient(url); var request = new RestSharp.RestRequest(); request.Method = RestSharp.Method.POST; request.RequestFormat = RestSharp.DataFormat.Json; request.AddHeader("Content-Type", "application/json;charset=utf-8"); request.AddHeader("Authorization", "Bearer " + token); request.AddBody(user); restClient.Execute(request); }
private User GetUser(ClaimsIdentity args) { var hostName = ApplicationConfiguration.Get("pandora_api_url"); var claims = args.Claims; var userId = claims.Where(x => x.Type == ClaimTypes.NameIdentifier).FirstOrDefault().Value; string token = claims.Where(x => x.Type == "id_token").FirstOrDefault().Value; var url = hostName + "/api/Users/" + userId; var restClient = new RestSharp.RestClient(url); var request = new RestSharp.RestRequest(); request.Method = RestSharp.Method.GET; request.AddHeader("Authorization", "Bearer " + token); var result = restClient.Execute(request); User user = null; if (string.IsNullOrWhiteSpace(result.Content) || result.Content.ToLowerInvariant() == "null") { user = new User(); user.Id = userId; user.Access = new SecurityAccess(); CreateUser(user, token); } else { user = JsonConvert.DeserializeObject<User>(result.Content); } return user; }