Asp.Net对Azure data lake和Azure SQL Server的读/写
我喜欢创建web应用程序来上传文件,保存到azure data lake,读/写到azure SQL Server 我使用Azure AD clientId/secret访问data lake 我的Azure SQL Server连接字符串,如:Server=tcp:{MyAzureSQLServer}.database.windows.net,1433;初始目录={MyAzureDatabase};持久安全信息=False;MultipleActiveResultSets=False;加密=真;TrustServerCertificate=False;身份验证=“Active Directory集成” Net Mvc核心,data lake运行良好,但Azure SQL report:不支持关键字:“身份验证” Asp.Net Mvc(框架),Azure SQL运行,但数据湖报告错误:“Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache”的类型初始值设定项引发异常 我做错了什么 谢谢, Wes使用访问令牌(通过托管标识获得)在SQL client for.NET Core中使用Azure Active Directory身份验证。下面是所有您需要的,包括连接字符串 Startup.cs:Asp.Net对Azure data lake和Azure SQL Server的读/写,asp.net,azure,azure-active-directory,azure-sql-database,azure-data-lake,Asp.net,Azure,Azure Active Directory,Azure Sql Database,Azure Data Lake,我喜欢创建web应用程序来上传文件,保存到azure data lake,读/写到azure SQL Server 我使用Azure AD clientId/secret访问data lake 我的Azure SQL Server连接字符串,如:Server=tcp:{MyAzureSQLServer}.database.windows.net,1433;初始目录={MyAzureDatabase};持久安全信息=False;MultipleActiveResultSets=False;加密=真
public void ConfigureServices(IServiceCollection services)
{
//code ignored for simplicity
services.AddDbContext<MyCustomDBContext>();
services.AddTransient<IDBAuthTokenService, AzureSqlAuthTokenService>();
}
public void配置服务(IServiceCollection服务)
{
//为简单起见,代码被忽略
services.AddDbContext();
services.AddTransient();
}
MyCustomDBContext.cs
public partial class MyCustomDBContext : DbContext
{
public IConfiguration Configuration { get; }
public IDBAuthTokenService authTokenService { get; set; }
public CortexContext(IConfiguration configuration, IDBAuthTokenService tokenService, DbContextOptions<MyCustomDBContext> options)
: base(options)
{
Configuration = configuration;
authTokenService = tokenService;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = Configuration.GetConnectionString("defaultConnection");
connection.AccessToken = authTokenService.GetToken().Result;
optionsBuilder.UseSqlServer(connection);
}
}
public class AzureSqlAuthTokenService : IDBAuthTokenService
{
public async Task<string> GetToken()
{
AzureServiceTokenProvider provider = new AzureServiceTokenProvider();
var token = await provider.GetAccessTokenAsync("https://database.windows.net/");
return token;
}
}
公共部分类MyCustomDBContext:DbContext
{
公共IConfiguration配置{get;}
公共IDBAuthTokenService authTokenService{get;set;}
公共CortexContext(IConfiguration配置、IDBAuthTokenService tokenService、DbContextOptions选项)
:基本(选项)
{
配置=配置;
authTokenService=tokenService;
}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
SqlConnection=newsqlconnection();
connection.ConnectionString=Configuration.GetConnectionString(“defaultConnection”);
connection.AccessToken=authTokenService.GetToken().Result;
optionsBuilder.UseSqlServer(连接);
}
}
azuresqlauthokenservice.cs
public partial class MyCustomDBContext : DbContext
{
public IConfiguration Configuration { get; }
public IDBAuthTokenService authTokenService { get; set; }
public CortexContext(IConfiguration configuration, IDBAuthTokenService tokenService, DbContextOptions<MyCustomDBContext> options)
: base(options)
{
Configuration = configuration;
authTokenService = tokenService;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = Configuration.GetConnectionString("defaultConnection");
connection.AccessToken = authTokenService.GetToken().Result;
optionsBuilder.UseSqlServer(connection);
}
}
public class AzureSqlAuthTokenService : IDBAuthTokenService
{
public async Task<string> GetToken()
{
AzureServiceTokenProvider provider = new AzureServiceTokenProvider();
var token = await provider.GetAccessTokenAsync("https://database.windows.net/");
return token;
}
}
公共类AzureSqlAuthTokenService:IDBAuthTokenService
{
公共异步任务GetToken()
{
AzureServiceTokenProvider=新的AzureServiceTokenProvider();
var token=wait provider.GetAccessTokenAsync(“https://database.windows.net/");
返回令牌;
}
}
更多关于使用访问令牌进行AAD身份验证的信息。谢谢你,Alberto,我假设这是针对Asp.Net Mvc Core的,对吗?.Net Core和EF CoreHi@AlbertoMorillo请指定IDBAuthTokenService使用了哪个Nuget包或命名空间。Thanks@Deepak他使用了这个:Microsoft.Azure.Services。AppAuthentication@Maturano谢谢你的回复,非常感谢。