Если вы хотите использовать Azure токен доступа AD для доступа к Azure озеру данных gen2, обратитесь к следующему коду
- , создайте субъект службы и назначьте Azure RAB * 1041. * роль для sp.
az login
az account set --subscription "<your subscription id>"
# it will assign Storage Blob Data Contributor to the sp at subscription level
az ad sp create-for-rbac -n "mysample" --role Storage Blob Data Contributor
![enter image description here](https://i.stack.imgur.com/1kOsX.png)
Код
string tokenUrl = $"https://login.microsoftonline.com/<tenantId>.onmicrosoft.com/oauth2/token";
var tokenRequest = new HttpRequestMessage(HttpMethod.Post, tokenUrl);
//I am Using client_credentials as It is mostly recommended
tokenRequest.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
["grant_type"] = "client_credentials",
["client_id"] = "--------",
["client_secret"] = "-------",
["resource"] = "https://storage.azure.com/"
});
dynamic json;
AccessTokenClass results = new AccessTokenClass();
HttpClient client = new HttpClient();
var tokenResponse = client.SendAsync(tokenRequest).GetAwaiter();
json = tokenResponse.GetResult().Content.ReadAsStringAsync().GetAwaiter();
results = JsonConvert.DeserializeObject<AccessTokenClass>(json);
Если вы хотите создать токен sas, выполните следующие действия:
получите ключ учетной записи через Azure Портал ![enter image description here](https://i.stack.imgur.com/y3CRQ.png)
код
var key = account key you copy";
var accountName = "testadls05";
StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, key);
AccountSasBuilder sas = new AccountSasBuilder
{
Protocol = SasProtocol.None,
Services = AccountSasServices.Blobs,
ResourceTypes = AccountSasResourceTypes.All,
StartsOn = DateTimeOffset.UtcNow.AddHours(-1),
ExpiresOn = DateTimeOffset.UtcNow.AddHours(1),
};
sas.SetPermissions(AccountSasPermissions.All);
var uri = $"https://{accountName}.dfs.core.windows.net/";
UriBuilder sasUri = new UriBuilder(uri);
sasUri.Query = sas.ToSasQueryParameters(credential).ToString();
DataLakeServiceClient service = new DataLakeServiceClient(sasUri.Uri);
var result =service.GetFileSystems().First();
Console.WriteLine(result.Name);