/// <summary> /// Executes the operations associated with the cmdlet. /// </summary> public override void ExecuteCmdlet() { PartnerCenter.Models.Customers.Customer customer; IValidator <Address> validator; string country; string culture; string region; if (ShouldProcess(string.Format(CultureInfo.CurrentCulture, Resources.NewPartnerCustomerWhatIf, Name))) { country = (string.IsNullOrEmpty(BillingAddressCountry)) ? PartnerSession.Instance.Context.CountryCode : BillingAddressCountry; culture = (string.IsNullOrEmpty(Culture)) ? PartnerSession.Instance.Context.Locale : Culture; if (string.IsNullOrEmpty(BillingAddressRegion)) { region = null; } else { region = BillingAddressRegion.Equals(UnitedStatesCountryCode, StringComparison.InvariantCultureIgnoreCase) ? string.Empty : BillingAddressRegion; } customer = new PartnerCenter.Models.Customers.Customer { AssociatedPartnerId = AssociatedPartnerId, BillingProfile = new PartnerCenter.Models.Customers.CustomerBillingProfile { CompanyName = Name, Culture = culture, DefaultAddress = new Address { AddressLine1 = BillingAddressLine1, AddressLine2 = BillingAddressLine2, City = BillingAddressCity, Country = country, FirstName = ContactFirstName, LastName = ContactLastName, PhoneNumber = ContactPhoneNumber, PostalCode = BillingAddressPostalCode, Region = region, State = BillingAddressState }, Email = ContactEmail, FirstName = ContactFirstName, Language = Language, LastName = ContactLastName }, CompanyProfile = new PartnerCenter.Models.Customers.CustomerCompanyProfile { CompanyName = Name, Domain = Domain } }; if (!DisableValidation.ToBool()) { if (Partner.Domains.ByDomain(Domain).ExistsAsync().GetAwaiter().GetResult()) { throw new PSInvalidOperationException( string.Format( CultureInfo.CurrentCulture, Resources.DomainExistsError, Domain)); } validator = new AddressValidator(Partner); if (!validator.IsValid(customer.BillingProfile.DefaultAddress, d => WriteDebug(d))) { throw new PartnerPSException("The address for the customer is not valid."); } } WriteObject(Partner.Customers.CreateAsync(customer).GetAwaiter().GetResult()); } }
/// <summary> /// Executes the operations associated with the cmdlet. /// </summary> public override void ExecuteCmdlet() { Scheduler.RunTask(async() => { if (ShouldProcess(string.Format(CultureInfo.CurrentCulture, Resources.NewPartnerCustomerWhatIf, Name))) { IPartner partner = await PartnerSession.Instance.ClientFactory.CreatePartnerOperationsAsync(CorrelationId, CancellationToken).ConfigureAwait(false); PartnerCenter.Models.Customers.Customer customer; string country = (string.IsNullOrEmpty(BillingAddressCountry)) ? PartnerSession.Instance.Context.CountryCode : BillingAddressCountry; string culture = (string.IsNullOrEmpty(Culture)) ? PartnerSession.Instance.Context.Locale : Culture; string region; if (string.IsNullOrEmpty(BillingAddressRegion)) { region = null; } else { region = BillingAddressRegion.Equals(UnitedStatesCountryCode, StringComparison.InvariantCultureIgnoreCase) ? string.Empty : BillingAddressRegion; } customer = new PartnerCenter.Models.Customers.Customer { AssociatedPartnerId = AssociatedPartnerId, BillingProfile = new PartnerCenter.Models.Customers.CustomerBillingProfile { CompanyName = Name, Culture = culture, DefaultAddress = new Address { AddressLine1 = BillingAddressLine1, AddressLine2 = BillingAddressLine2, City = BillingAddressCity, Country = country, FirstName = ContactFirstName, LastName = ContactLastName, PhoneNumber = ContactPhoneNumber, PostalCode = BillingAddressPostalCode, Region = region, State = BillingAddressState }, Email = ContactEmail, FirstName = ContactFirstName, Language = Language, LastName = ContactLastName }, CompanyProfile = new PartnerCenter.Models.Customers.CustomerCompanyProfile { CompanyName = Name, Domain = Domain } }; if (!DisableValidation.ToBool()) { if (await partner.Domains.ByDomain(Domain).ExistsAsync(CancellationToken).ConfigureAwait(false)) { throw new PSInvalidOperationException( string.Format( CultureInfo.CurrentCulture, Resources.DomainExistsError, Domain)); } IValidator <Address> validator = new AddressValidator(partner); if (!await validator.IsValidAsync(customer.BillingProfile.DefaultAddress, CancellationToken).ConfigureAwait(false)) { throw new PartnerPowerShellException("The address for the customer is not valid.", PartnerPowerShellErrorCategory.Validation); } } WriteObject(await partner.Customers.CreateAsync(customer).ConfigureAwait(false)); } }, true); }
/// <summary> /// Configures application authentication. /// </summary> /// <param name="app">The application to configure.</param> public void ConfigureAuth(IAppBuilder app) { app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.UseCookieAuthentication(new CookieAuthenticationOptions { }); app.UseOpenIdConnectAuthentication( new OpenIdConnectAuthenticationOptions { ClientId = ApplicationConfiguration.ActiveDirectoryClientID, Authority = ApplicationConfiguration.ActiveDirectoryEndPoint + "common", TokenValidationParameters = new TokenValidationParameters() { // instead of using the default validation (validating against a single issuer value, as we do in line of business apps), // we inject our own multitenant validation logic ValidateIssuer = false, }, Notifications = new OpenIdConnectAuthenticationNotifications() { RedirectToIdentityProvider = (context) => { context.ProtocolMessage.Parameters.Add("lc", Resources.Culture.LCID.ToString(CultureInfo.InvariantCulture)); return(Task.FromResult(0)); }, AuthorizationCodeReceived = async(context) => { string userTenantId = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; string signedInUserObjectId = context.AuthenticationTicket.Identity.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; IGraphClient graphClient = new GraphClient( userTenantId, context.Code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path))); List <RoleModel> roles = await graphClient.GetDirectoryRolesAsync(signedInUserObjectId).ConfigureAwait(false); foreach (RoleModel role in roles) { context.AuthenticationTicket.Identity.AddClaim(new System.Security.Claims.Claim(System.Security.Claims.ClaimTypes.Role, role.DisplayName)); } if (userTenantId != ApplicationConfiguration.ActiveDirectoryTenantId) { string partnerCenterCustomerId = string.Empty; // Check to see if this login came from the tenant of a customer of the partner PartnerCenter.Models.Customers.Customer customerDetails = await ApplicationDomain.Instance.PartnerCenterClient.Customers.ById(userTenantId).GetAsync().ConfigureAwait(false); // indeed a customer partnerCenterCustomerId = customerDetails.Id; if (!string.IsNullOrWhiteSpace(partnerCenterCustomerId)) { // add the customer ID to the claims context.AuthenticationTicket.Identity.AddClaim(new System.Security.Claims.Claim("PartnerCenterCustomerID", partnerCenterCustomerId)); // fire off call to retrieve this customer's subscriptions and populate the CustomerSubscriptions Repository. } } else { if (context.AuthenticationTicket.Identity.FindFirst(System.Security.Claims.ClaimTypes.Role).Value != Startup.GlobalAdminUserRole) { // this login came from the partner's tenant, only allow admins to access the site, non admins will only // see the unauthenticated experience but they can't configure the portal nor can purchase Trace.TraceInformation("Blocked log in from non admin partner user: {0}", signedInUserObjectId); throw new UnauthorizedException(Resources.NonAdminUnauthorizedMessage, HttpStatusCode.Unauthorized); } } }, AuthenticationFailed = (context) => { // redirect to the error page string errorMessage = (context.Exception.InnerException == null) ? context.Exception.Message : context.Exception.InnerException.Message; context.OwinContext.Response.Redirect($"/Home/Error?errorMessage={errorMessage}"); context.HandleResponse(); return(Task.FromResult(0)); } } }); }