久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用托管標識與 Azure SQL 的 EF Core 連接

EF Core Connection to Azure SQL with Managed Identity(使用托管標識與 Azure SQL 的 EF Core 連接)
本文介紹了使用托管標識與 Azure SQL 的 EF Core 連接的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在使用 EF Core 連接到部署到 Azure 應用服務的 Azure SQL 數據庫.我正在使用訪問令牌(通過托管身份獲得)連接到 Azure SQL 數據庫.

I am using EF Core to connect to a Azure SQL Database deployed to Azure App Services. I am using an access token (obtained via the Managed Identities) to connect to Azure SQL database.

這是我的做法:

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    //code ignored for simplicity
    services.AddDbContext<MyCustomDBContext>();

    services.AddTransient<IDBAuthTokenService, AzureSqlAuthTokenService>();
}

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);
    }
}

AzureSqlAuthTokenService.cs

public class AzureSqlAuthTokenService : IDBAuthTokenService
{
    public async Task<string> GetToken()
    {
        AzureServiceTokenProvider provider = new AzureServiceTokenProvider();
        var token = await provider.GetAccessTokenAsync("https://database.windows.net/");

        return token;
    }
}

這很好,我可以從數據庫中獲取數據.但我不確定這是否是正確的做法.

This works fine and I can get data from the database. But I am not sure if this is the right way to do it.

我的問題:

  1. 這是一種正確的方法嗎?還是會出現性能問題?
  2. 我需要擔心令牌過期嗎?我現在沒有緩存令牌.
  3. EF Core 有沒有更好的方法來處理這個問題?

推薦答案

這是一種正確的方法嗎?還是會出現性能問題?

Is this a right way to do it or will it have issues with performance?

這是正確的方法.為每個新的 DbContext 調用 OnConfiguring,因此假設您沒有任何長期存在的 DbContext 實例,這是正確的模式.

That is the right way. OnConfiguring is called for each new DbContext, so assuming you don't have any long-lived DbContext instances, this is the right pattern.

我需要擔心令牌過期嗎?我現在沒有緩存令牌.

Do I need to worry about token expiration? I am not caching the token as of now.

AzureServiceTokenProvider 負責緩存.

EF Core 有沒有更好的方法來處理這個問題?

Does EF Core has any better way to handle this?

設置 SqlConnection.AccessToken 是目前在 .NET Core 的 SqlClient 中使用 AAD Auth 的唯一方法.

Setting the SqlConnection.AccessToken is currently the only way of using AAD Auth in SqlClient for .NET Core.

這篇關于使用托管標識與 Azure SQL 的 EF Core 連接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 黄色网络在线观看 | 国产精品久久久久久久久久久久 | 欧美专区在线 | 久久久久久99 | 色综合一区二区 | 国产99热在线 | 久久国产传媒 | 日韩精品一区二区三区视频播放 | 欧美一区二区成人 | 久久亚洲春色中文字幕久久久 | www亚洲精品 | 国产福利在线免费观看 | 久久久久久亚洲国产精品 | 伊人在线| 最近中文字幕免费 | 国产激情第一页 | 国产成人免费在线观看 | 美日韩一区二区 | 日韩精品视频在线观看一区二区三区 | 日韩高清中文字幕 | 国产精品久久久久久一区二区三区 | 狠狠操天天干 | 毛片网站在线观看 | 亚洲www啪成人一区二区 | 亚洲日韩第一页 | 亚洲国产成人精品女人 | 国产久| 欧美日韩国产精品一区 | 国产91在线观看 | 亚洲综合成人网 | 国产精品久久久久久吹潮日韩动画 | 九九九精品视频 | 福利网址| 国产精品无码专区在线观看 | 日韩三级在线 | 九九在线视频 | 精品欧美一区二区三区精品久久 | 午夜成人在线视频 | 成人美女免费网站视频 | 色婷婷亚洲国产女人的天堂 | 国户精品久久久久久久久久久不卡 |