Skip to content

Commit

Permalink
Allow TokenCredentials to be passed in (supporting Managed Identities…
Browse files Browse the repository at this point in the history
… instead of Shared Access Keys)
  • Loading branch information
MatthewSteeples committed Jan 30, 2024
1 parent 1799ac8 commit c81af12
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// MIT License Copyright 2020 (c) David Melendez. All rights reserved. See License.txt in the project root for license information.


using System;
using Azure.Core;

namespace ElCamino.AspNetCore.Identity.AzureTable.Model
{
/// <summary>
Expand Down Expand Up @@ -33,5 +36,8 @@ public class IdentityConfiguration
/// </summary>
public string? RoleTableName { get; set; }

public Uri? StorageConnectionUri { get; set; }
public TokenCredential? TokenCredential { get; set; }

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,35 @@ public IdentityCloudContext(IdentityConfiguration config)
}
#endif

_client = new TableServiceClient(config.StorageConnectionString);
_indexTable = _client.GetTableClient(FormatTableNameWithPrefix(config!.TablePrefix, !string.IsNullOrWhiteSpace(config!.IndexTableName) ? config!.IndexTableName! : TableConstants.TableNames.IndexTable));
_roleTable = _client.GetTableClient(FormatTableNameWithPrefix(config!.TablePrefix, !string.IsNullOrWhiteSpace(config!.RoleTableName) ? config!.RoleTableName! : TableConstants.TableNames.RolesTable));
_userTable = _client.GetTableClient(FormatTableNameWithPrefix(config!.TablePrefix, !string.IsNullOrWhiteSpace(config!.UserTableName) ? config!.UserTableName! : TableConstants.TableNames.UsersTable));
if (string.IsNullOrEmpty(config.StorageConnectionString) && config.StorageConnectionUri == null)
{
throw new ArgumentNullException(nameof(config.StorageConnectionString), "Either StorageConnectionString or StorageConnectionUri are required");
}
else if (!string.IsNullOrEmpty(config.StorageConnectionString))
{
_client = new TableServiceClient(config.StorageConnectionString);

if (config.TokenCredential != null)
{
//If we've been passed a TokenCredential we can use that instead of the credentials in the connection string
_client = new TableServiceClient(_client.Uri, config.TokenCredential);
}
}
else // if (config.StorageConnectionUri != null)
{
if (config.TokenCredential == null)
{
throw new ArgumentNullException(nameof(config.TokenCredential), "TokenCredential is required when Uri is specified");
}
else
{
_client = new TableServiceClient(config.StorageConnectionUri, config.TokenCredential);
}
}

_indexTable = _client.GetTableClient(FormatTableNameWithPrefix(config.TablePrefix, !string.IsNullOrWhiteSpace(config.IndexTableName) ? config.IndexTableName : TableConstants.TableNames.IndexTable));
_roleTable = _client.GetTableClient(FormatTableNameWithPrefix(config.TablePrefix, !string.IsNullOrWhiteSpace(config.RoleTableName) ? config.RoleTableName : TableConstants.TableNames.RolesTable));
_userTable = _client.GetTableClient(FormatTableNameWithPrefix(config.TablePrefix, !string.IsNullOrWhiteSpace(config.UserTableName) ? config.UserTableName : TableConstants.TableNames.UsersTable));
}

private static string FormatTableNameWithPrefix(string? tablePrefix, string baseTableName)
Expand Down

0 comments on commit c81af12

Please sign in to comment.