本文整理汇总了C#中Microsoft.Azure.Common.Authentication.Models.AzureAccount.SetOrAppendProperty方法的典型用法代码示例。如果您正苦于以下问题:C# AzureAccount.SetOrAppendProperty方法的具体用法?C# AzureAccount.SetOrAppendProperty怎么用?C# AzureAccount.SetOrAppendProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Azure.Common.Authentication.Models.AzureAccount
的用法示例。
在下文中一共展示了AzureAccount.SetOrAppendProperty方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Login
public AzureRMProfile Login(
AzureAccount account,
AzureEnvironment environment,
string tenantId,
string subscriptionId,
string subscriptionName,
SecureString password)
{
AzureSubscription newSubscription = null;
AzureTenant newTenant = null;
ShowDialog promptBehavior =
(password == null &&
account.Type != AzureAccount.AccountType.AccessToken &&
!account.IsPropertySet(AzureAccount.Property.CertificateThumbprint))
? ShowDialog.Always : ShowDialog.Never;
// (tenant and subscription are present) OR
// (tenant is present and subscription is not provided)
if (!string.IsNullOrEmpty(tenantId))
{
var token = AcquireAccessToken(account, environment, tenantId, password, promptBehavior);
if(TryGetTenantSubscription(token, account, environment, tenantId, subscriptionId, subscriptionName, out newSubscription, out newTenant))
{
account.SetOrAppendProperty(AzureAccount.Property.Tenants, new[] { newTenant.Id.ToString() });
}
}
// (tenant is not provided and subscription is present) OR
// (tenant is not provided and subscription is not provided)
else
{
var tenants = ListAccountTenants(account, environment, password, promptBehavior).Select(s => s.Id.ToString()).ToArray();
account.SetProperty(AzureAccount.Property.Tenants, null);
string accountId = null;
for (int i = 0; i < tenants.Count(); i++)
{
var tenant = tenants[i];
AzureTenant tempTenant;
AzureSubscription tempSubscription;
IAccessToken token = null;
try
{
token = AcquireAccessToken(account, environment, tenant, password, ShowDialog.Auto);
if (accountId == null)
{
accountId = account.Id;
account.SetOrAppendProperty(AzureAccount.Property.Tenants, tenant);
}
else if (accountId.Equals(account.Id, StringComparison.OrdinalIgnoreCase))
{
account.SetOrAppendProperty(AzureAccount.Property.Tenants, tenant);
}
else
{ // if account ID is different from the first tenant account id we need to ignore current tenant
WriteWarningMessage(string.Format(
Microsoft.Azure.Commands.Profile.Properties.Resources.AccountIdMismatch,
account.Id,
tenant,
accountId));
account.Id = accountId;
token = null;
}
}
catch
{
WriteWarningMessage(string.Format(Microsoft.Azure.Commands.Profile.Properties.Resources.UnableToAqcuireToken, tenant));
}
if (token != null &&
newTenant == null &&
TryGetTenantSubscription(token, account, environment, tenant, subscriptionId, subscriptionName, out tempSubscription, out tempTenant))
{
newTenant = tempTenant;
newSubscription = tempSubscription;
}
}
}
if (newSubscription == null)
{
if (subscriptionId != null)
{
throw new PSInvalidOperationException(String.Format(Properties.Resources.SubscriptionIdNotFound, account.Id, subscriptionId));
}
else if (subscriptionName != null)
{
throw new PSInvalidOperationException(String.Format(Properties.Resources.SubscriptionNameNotFound, account.Id, subscriptionName));
}
_profile.Context = new AzureContext(account, environment, newTenant);
}
else
{
_profile.Context = new AzureContext(newSubscription, account, environment, newTenant);
}
//.........这里部分代码省略.........
示例2: ListSubscriptionsFromServer
private IEnumerable<AzureSubscription> ListSubscriptionsFromServer(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior)
{
string[] tenants = null;
try
{
if (!account.IsPropertySet(AzureAccount.Property.Tenants))
{
tenants = LoadAccountTenants(account, environment, password, promptBehavior);
}
else
{
var storedTenants = account.GetPropertyAsArray(AzureAccount.Property.Tenants);
if (account.Type == AzureAccount.AccountType.User && storedTenants.Count() == 1)
{
TracingAdapter.Information(Resources.AuthenticatingForSingleTenant, account.Id, storedTenants[0]);
AzureSession.AuthenticationFactory.Authenticate(account, environment, storedTenants[0], password,
promptBehavior);
}
}
}
catch (AadAuthenticationException aadEx)
{
WriteOrThrowAadExceptionMessage(aadEx);
return new AzureSubscription[0];
}
try
{
tenants = tenants ?? account.GetPropertyAsArray(AzureAccount.Property.Tenants);
List<AzureSubscription> rdfeSubscriptions = ListServiceManagementSubscriptions(account, environment,
password, ShowDialog.Never, tenants).ToList();
// Set user ID
foreach (var subscription in rdfeSubscriptions)
{
account.SetOrAppendProperty(AzureAccount.Property.Subscriptions, subscription.Id.ToString());
}
if (rdfeSubscriptions.Any())
{
return rdfeSubscriptions;
}
else
{
return new AzureSubscription[0];
}
}
catch (AadAuthenticationException aadEx)
{
WriteOrThrowAadExceptionMessage(aadEx);
return new AzureSubscription[0];
}
}
示例3: ImportPublishSettings
public List<AzureSubscription> ImportPublishSettings(string filePath, string environmentName)
{
var subscriptions = ListSubscriptionsFromPublishSettingsFile(filePath, environmentName);
if (subscriptions.Any())
{
foreach (var subscription in subscriptions)
{
AzureAccount account = new AzureAccount
{
Id = subscription.Account,
Type = AzureAccount.AccountType.Certificate
};
account.SetOrAppendProperty(AzureAccount.Property.Subscriptions, subscription.Id.ToString());
AddOrSetAccount(account);
if (!Profile.Subscriptions.ContainsKey(subscription.Id))
{
AddOrSetSubscription(subscription);
}
if (Profile.DefaultSubscription == null)
{
Profile.DefaultSubscription = subscription;
}
}
}
return subscriptions;
}
示例4: ExecuteCmdlet
/// <summary>
/// Executes the set subscription cmdlet operation.
/// </summary>
public override void ExecuteCmdlet()
{
AzureSubscription subscription = null;
if (!string.IsNullOrEmpty(SubscriptionId) && string.IsNullOrEmpty(SubscriptionName))
{
subscription = ProfileClient.GetSubscription(new Guid(SubscriptionId));
Environment = subscription.Environment;
}
else if (string.IsNullOrEmpty(SubscriptionId) && !string.IsNullOrEmpty(SubscriptionName))
{
subscription = ProfileClient.GetSubscription(SubscriptionName);
Environment = subscription.Environment;
}
else
{
subscription = new AzureSubscription();
subscription.Id = new Guid(SubscriptionId);
subscription.Name = SubscriptionName;
}
AzureEnvironment environment = ProfileClient.GetEnvironment(Environment, ServiceEndpoint, ResourceManagerEndpoint);
if (environment == null)
{
var profileClient = new ProfileClient(Profile);
environment = profileClient.GetEnvironment(Environment, ServiceEndpoint, ResourceManagerEndpoint);
}
if (environment == null)
{
throw new ArgumentException("ServiceEndpoint and ResourceManagerEndpoint values do not "+
"match existing environment. Please use Environment parameter.");
}
else
{
subscription.Environment = environment.Name;
}
if (ServiceEndpoint != null || ResourceManagerEndpoint != null)
{
WriteWarning("Please use Environment parameter to specify subscription environment. This "+
"warning will be converted into an error in the upcoming release.");
}
if (Certificate != null)
{
ProfileClient.ImportCertificate(Certificate);
subscription.Account = Certificate.Thumbprint;
AzureAccount account = new AzureAccount
{
Id = Certificate.Thumbprint,
Type = AzureAccount.AccountType.Certificate
};
account.SetOrAppendProperty(AzureAccount.Property.Subscriptions, subscription.Id.ToString());
ProfileClient.AddOrSetAccount(account);
if (subscription.Account == null)
{
subscription.Account = account.Id;
}
}
if (subscription.Account == null)
{
throw new ArgumentException("Certificate is required for creating a new subscription.");
}
if (!string.IsNullOrEmpty(CurrentStorageAccountName))
{
subscription.Properties[AzureSubscription.Property.StorageAccount] = CurrentStorageAccountName;
}
subscription = ProfileClient.AddOrSetSubscription(subscription);
if (PassThru)
{
WriteObject(subscription);
}
}
示例5: InitializeAzureProfile
private void InitializeAzureProfile(AzureProfile profile, string parameterSet, AzureProfileSettings settings)
{
var savedCache = AzureSession.TokenCache;
AzureSession.TokenCache = DefaultMemoryTokenCache;
try
{
var profileClient = new ProfileClient(profile);
if (settings.Environment == null)
{
settings.Environment = AzureEnvironment.PublicEnvironments["AzureCloud"];
}
switch (parameterSet)
{
case CertificateParameterSet:
profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId),
settings.Certificate,
settings.StorageAccount);
break;
case CredentialsParameterSet:
var userAccount = new AzureAccount
{
Id = settings.Credential.UserName,
Type = AzureAccount.AccountType.User
};
profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId),
userAccount,
settings.Credential.Password, settings.StorageAccount);
break;
case AccessTokenParameterSet:
profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId),
settings.AccessToken,
settings.AccountId, settings.StorageAccount);
break;
case ServicePrincipalParameterSet:
var servicePrincipalAccount = new AzureAccount
{
Id = settings.Credential.UserName,
Type = AzureAccount.AccountType.ServicePrincipal,
};
servicePrincipalAccount.SetOrAppendProperty(AzureAccount.Property.Tenants, settings.Tenant);
profileClient.InitializeProfile(settings.Environment, new Guid(settings.SubscriptionId),
servicePrincipalAccount,
settings.Credential.Password, settings.StorageAccount);
break;
case EmptyParameterSet:
if (!profile.Environments.ContainsKey(settings.Environment.Name))
{
profile.Environments.Add(settings.Environment.Name, settings.Environment);
}
break;
}
}
finally
{
AzureSession.TokenCache = savedCache;
}
}
示例6: ListSubscriptionsFromServer
private IEnumerable<AzureSubscription> ListSubscriptionsFromServer(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior)
{
string[] tenants = null;
try
{
if (!account.IsPropertySet(AzureAccount.Property.Tenants))
{
tenants = LoadAccountTenants(account, environment, password, promptBehavior);
}
}
catch (AadAuthenticationException aadEx)
{
WriteOrThrowAadExceptionMessage(aadEx);
return new AzureSubscription[0];
}
try
{
tenants = tenants ?? account.GetPropertyAsArray(AzureAccount.Property.Tenants);
List<AzureSubscription> mergedSubscriptions = MergeSubscriptions(
ListServiceManagementSubscriptions(account, environment, password, ShowDialog.Never, tenants).ToList(),
ListResourceManagerSubscriptions(account, environment, password, ShowDialog.Never, tenants).ToList());
// Set user ID
foreach (var subscription in mergedSubscriptions)
{
account.SetOrAppendProperty(AzureAccount.Property.Subscriptions, subscription.Id.ToString());
}
if (mergedSubscriptions.Any())
{
return mergedSubscriptions;
}
else
{
return new AzureSubscription[0];
}
}
catch (AadAuthenticationException aadEx)
{
WriteOrThrowAadExceptionMessage(aadEx);
return new AzureSubscription[0];
}
}