Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow TokenCredentials to be passed in Config #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,17 +30,42 @@ 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)
{
if (!string.IsNullOrWhiteSpace(tablePrefix))
{
return string.Format("{0}{1}", tablePrefix!, baseTableName);
return string.Format("{0}{1}", tablePrefix, baseTableName);
}
return baseTableName;
}
Expand Down