我正在编写一个二进制cmdlet,以将记录添加到CosmosDb集合中。我需要用户登录到Azure(用户凭据),并且可以使用PS命令来获取其余信息。 我可以在Powershell中使用Get-AzCosmosDbAccount进行此操作,因为该人在Azure中具有查看资源的权限。
我找不到在C#代码中执行此操作的方法。我发现了几个非常接近但无法实际工作的示例。例如,我在用于.NET的Azure SDK中找到了this example,但是找不到对import android.graphics.Path;
import android.os.Parcel;
import android.os.Parcelable;
public class FingerPath implements Parcelable {
public int color;
public boolean emboss;
public boolean blur;
public int strokeWidth;
public Path path;
public FingerPath(int color, boolean emboss, boolean blur, int strokeWidth, Path path) {
this.color = color;
this.emboss = emboss;
this.blur = blur;
this.strokeWidth = strokeWidth;
this.path = path;
}
protected FingerPath(Parcel in) {
color = in.readInt();
emboss = in.readByte() != 0;
blur = in.readByte() != 0;
strokeWidth = in.readInt();
}
public static final Creator<FingerPath> CREATOR = new Creator<FingerPath>() {
@Override
public FingerPath createFromParcel(Parcel in) {
return new FingerPath(in);
}
@Override
public FingerPath[] newArray(int size) {
return new FingerPath[size];
}
};
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(color);
parcel.writeByte((byte) (emboss ? 1 : 0));
parcel.writeByte((byte) (blur ? 1 : 0));
parcel.writeInt(strokeWidth);
}
}
库的引用。
我找到了this example,但它使用应用程序注册凭据来认证用户凭据。我需要使用用户凭据。
我想在C#中执行此PS脚本:
Azure.ResourceManager.Resources
可悲的是,我无法理解的是Login-AzAccount
Get-AzSubscription -SubscriptionId xxxxxxxx-xxxx-xxxx-xxxx-cxxxxxxxxxxx | Select-AzSubscription
$cosmosKey = Get-AzCosmosDBAccountKey -ResourceGroupName 'rg-temp' -Name 'doctemp'
$cosmosKey.PrimaryMasterKey
。
答案 0 :(得分:1)
关键是使用 new DefaultAzureCredential(true)
- true
还将为用户凭据启用交互式身份验证。您还可以查看 this document 以了解有关身份验证的一般信息。
以下示例使用 nuget 包 Azure.Identity 和 Azure.ResourceManager.CosmosDB。 后者目前仅作为预发布版本提供。你也可以试试Microsoft.Azure.Management.CosmosDB。
var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
var client = new Azure.ResourceManager.CosmosDB.CosmosDBManagementClient(
subscriptionId, new DefaultAzureCredential(true));
var keys = client.DatabaseAccounts.ListKeys("resourcegroupName", "accountname");
如果您需要切换到不同的租户,您可以设置 AZURE_TENANT_ID
环境变量。
答案 1 :(得分:0)
您需要的是Microsoft.Azure.Cosmos
软件包。使用它,您可以使用CosmosClient
类连接到Cosmos DB:
this.cosmosClient = new CosmosClient(EndpointUri, PrimaryKey);
您可以查看更详细的教程here。