public string AddServicePrinciple(string email, ServicePrinciple servicePrinciple) { AccountDataModel accountDataModel = _accountData.Find(s => s.Email == email).FirstOrDefault(); List <ServicePrinciple> servicePrinciples = accountDataModel.servicePrinciples; if (servicePrinciples != null) { ServicePrinciple service = servicePrinciples.Find(s => s.TenantId == servicePrinciple.TenantId && s.ClientId == servicePrinciple.ClientId); if (service == null) { accountDataModel.servicePrinciples.Add(servicePrinciple); } else { return("service is already Exist"); } } else { List <ServicePrinciple> temp_servicePrinciples = new List <ServicePrinciple>(); temp_servicePrinciples.Add(servicePrinciple); accountDataModel.servicePrinciples = temp_servicePrinciples; } var filter = Builders <AccountDataModel> .Filter.Eq("Email", email); var update = Builders <AccountDataModel> .Update.Set("ServicePrinciples", accountDataModel.servicePrinciples); //BsonDocument bd = BsonExtensionMethods.ToBsonDocument(accountDataModel); _accountData.UpdateOne(filter, update); return("success"); }
public ServicePrinciple GetCurrentService(string email) { AccountDataModel accountDataModel = _accountData.Find(s => s.Email == email).FirstOrDefault(); ServicePrinciple servicePrinciple = accountDataModel.servicePrinciples.Find(s => s.flag == true); return(servicePrinciple); }
public async Task <object> CreateAKS(string email, string subid, string resourceGroupName, CreateAKSModel createAKSModel) { string access_token = _tokenDto.GetTokenString(email, _tokenResource.Value.manage); ServicePrinciple servicePrinciple = _accountDao.GetCurrentService(email); return(await this._aKSDto.CreateAKS(subid, resourceGroupName, createAKSModel, servicePrinciple.ClientId, servicePrinciple.ClientSecret, access_token)); }
public string UpdateClientSecret(string email, ServicePrinciple servicePrinciple) { AccountDataModel accountDataModel = _accountData.Find(s => s.Email == email).FirstOrDefault(); accountDataModel.servicePrinciples.Find(s => s.TenantId == servicePrinciple.TenantId && s.ClientId == servicePrinciple.ClientId).ClientSecret = servicePrinciple.ClientSecret; var filter = Builders <AccountDataModel> .Filter.Eq("Email", email); var update = Builders <AccountDataModel> .Update.Set("ServicePrinciples", accountDataModel.servicePrinciples); //BsonDocument bd = BsonExtensionMethods.ToBsonDocument(accountDataModel); _accountData.UpdateOne(filter, update); return("success"); }
public async Task <TokenModel> GetToken(string email, string resource) { ServicePrinciple servicePrinciple = this._accountDao.GetCurrentService(email); //string clientId = this._configuration["accountsetting:clientId"]; //string clientSecret = this._configuration["accountsetting:clientSecret"]; //string resource = this._configuration["accountsetting:resource"]; //string tenantId = this._configuration["accountsetting:tenantId"]; //string tenantId = "c7917735-5d61-4832-8b54-b11d5f1e7810"; string tenantId = servicePrinciple.TenantId; //string clientSecret = "77b650d9-f8d3-4511-8587-c6c930e05225"; string clientSecret = servicePrinciple.ClientSecret; //string resource = "https://management.chinacloudapi.cn"; string clientId = servicePrinciple.ClientId; //string clientId = "57d1ea2f-7ba4-4d03-936a-036368ff957c"; List <KeyValuePair <string, string> > vals = new List <KeyValuePair <string, string> >(); vals.Add(new KeyValuePair <string, string>("client_id", clientId)); vals.Add(new KeyValuePair <string, string>("resource", resource)); vals.Add(new KeyValuePair <string, string>("grant_type", "client_credentials")); vals.Add(new KeyValuePair <string, string>("client_secret", clientSecret)); string tokenUrl = string.Format("https://login.chinacloudapi.cn/{0}/oauth2/token", tenantId); TokenModel TokenResponse = null; using (HttpClient httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache"); HttpContent content = new FormUrlEncodedContent(vals); HttpResponseMessage hrm = httpClient.PostAsync(tokenUrl, content).Result; if (hrm.IsSuccessStatusCode) { string data = await hrm.Content.ReadAsStringAsync(); TokenResponse = JsonConvert.DeserializeObject <TokenModel>(data); Console.WriteLine(TokenResponse); //await DataOperations(authenticationResponse); } else { Console.WriteLine("Error." + hrm.ReasonPhrase); } } return(TokenResponse); }
public string SetServicePrinciple(string email, string tenantId, string clientId) { AccountDataModel accountDataModel = _accountData.Find(s => s.Email == email).FirstOrDefault(); ServicePrinciple current_servicePrinciple = accountDataModel.servicePrinciples.Find(s => s.flag == true); ServicePrinciple servicePrinciple = accountDataModel.servicePrinciples.Find(s => s.TenantId == tenantId && s.ClientId == clientId); if (current_servicePrinciple == null) { servicePrinciple.flag = true; } else { current_servicePrinciple.flag = false; servicePrinciple.flag = true; } var filter = Builders <AccountDataModel> .Filter.Eq("Email", email); var update = Builders <AccountDataModel> .Update.Set("ServicePrinciples", accountDataModel.servicePrinciples); _accountData.UpdateOne(filter, update); return("success"); }
public async Task <List <ApplicationRegistration> > GetApplications() { // Get Applications For Current Tenant var applicationsJson = await GetFromGraph("/applications"); var graphObject = JsonConvert.DeserializeObject <GraphApplicationsResult>(applicationsJson); var applications = graphObject.Applications.ToList(); while (!string.IsNullOrEmpty(graphObject.OdataNextLink)) { var nextResult = await GetFromGraph(graphObject.OdataNextLink.Replace("https://graph.microsoft.com/beta", "")); graphObject = JsonConvert.DeserializeObject <GraphApplicationsResult>(nextResult); applications.AddRange(graphObject.Applications); } if (applications == null) { throw new InvalidOperationException("No Applications Found in your tenant"); } var principleCache = new List <ServicePrinciple>(); var applicationRegistrations = new List <ApplicationRegistration>(); foreach (var app in applications) { var application = new ApplicationRegistration { Id = app.AppId, DisplayName = app.DisplayName }; if (app.PasswordCredentials.Any()) { application.Secrets = app.PasswordCredentials.Select(s => $"{s.DisplayName} - Expires on: {s.EndDateTime.ToString("MM-dd-yyyy")}").ToList(); } if (app.KeyCredentials.Any()) { application.Certificates = app.KeyCredentials.Select(x => $"{x.CustomKeyIdentifier} - Expires on: {x.EndDateTime.ToString("MM-dd-yyyy")}").ToList(); } var ownersJson = await GetFromGraph($"/applications/{app.Id}/owners"); var owners = JsonConvert.DeserializeObject <GraphOwnerResult>(ownersJson).Value; application.Owners = owners.Select(x => x.UserPrincipalName).ToList(); foreach (var requiredAccess in app.RequiredResourceAccess) { // Retrieve the service principle form cache or when not found from the graph api ServicePrinciple currentPrinciple = null; if (principleCache.Any(x => x.AppId == requiredAccess.ResourceAppId)) { currentPrinciple = principleCache.First(x => x.AppId == requiredAccess.ResourceAppId); } else { var getServicePrinciple = $"/servicePrincipals?filter=appId eq '{requiredAccess.ResourceAppId}'"; var principleFromGraph = await GetFromGraph(getServicePrinciple); currentPrinciple = JsonConvert.DeserializeObject <ServicePrinciplesGraphResult>(principleFromGraph).ServicePrinciples?.FirstOrDefault(); if (currentPrinciple == null) { continue; } principleCache.Add(currentPrinciple); } // Approles -> Application Permissions // PermissionScopes -> Delegated Permissions var applicationRegistrationServices = new ApplicationRegistrationServices { DisplayName = currentPrinciple.AppDisplayName, DelegatedPermissions = currentPrinciple.PublishedPermissionScopes .Where(x => requiredAccess.ResourceAccess.Any(r => r.Id == x.Id)) .Select(s => new DelegatedPermission { DisplayName = s.UserConsentDisplayName, Id = s.Id, Permission = s.Value }), ApplicationPermissions = currentPrinciple.AppRoles .Where(x => requiredAccess.ResourceAccess.Any(r => r.Id == x.Id)) .Select(s => new ApplicationPermission { DisplayName = s.DisplayName, Id = s.Id, Permission = s.Value }) }; application.Services.Add(applicationRegistrationServices); } applicationRegistrations.Add(application); } return(applicationRegistrations); }
public string UpdateClientSeret(string email, [FromBody] ServicePrinciple servicePrinciple) { return(this._accountBus.UpdateClientSecret(email, servicePrinciple)); }
public string AddServicePrinciple(string email, [FromBody] ServicePrinciple servicePrinciple) { return(_accountBus.AddServicePrinciple(email, servicePrinciple)); }
public string UpdateClientSecret(string email, ServicePrinciple servicePrinciple) { return(this._accountDao.UpdateClientSecret(email, servicePrinciple)); }
/*public AccountModel GetUserInfo(string email) * { * AccountDataModel accountDataModel = this._accountDao.GetUserByEmail(email); * AccountModel accountModel = new AccountModel * { * email = accountDataModel.Email, * tenantId=accountDataModel.TenantId, * subscriptionId=accountDataModel.SubscriptionId, * clientId=accountDataModel.ClientId * }; * return accountModel; * }*/ public string AddServicePrinciple(string email, ServicePrinciple servicePrinciple) { return(this._accountDao.AddServicePrinciple(email, servicePrinciple)); }