久久久久久久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 超時)
主站蜘蛛池模板: a级免费视频 | 午夜视频精品 | 精品区一区二区 | 欧美日韩一二三区 | 欧美综合一区二区三区 | 欧美日韩一区二区三区四区 | 亚洲一区二区三区福利 | 久久久精品网站 | 九九久久精品 | 午夜天堂精品久久久久 | 国产偷录视频叫床高潮对白 | 中文字幕亚洲视频 | 不卡一区| 色久在线 | 韩日精品一区 | 亚洲精品中文字幕 | 国产精品久久久久久久久动漫 | 91精品91久久久 | 成人超碰在线 | 亚洲一区二区三区 | 69精品久久久久久 | 污片在线免费观看 | 成人av一区 | 69av网| 亚洲一区二区三区在线 | 亚洲网站在线 | 亚洲精品性视频 | 久草视频在 | 大象视频一区二区 | 日韩欧美专区 | 国产精品久久久久久久久久久久久久 | 中文字幕日韩一区 | 亚洲综合久久精品 | 精品欧美一区二区久久久伦 | 一级片免费网站 | 成人精品一区二区三区 | 亚洲不卡一 | 福利电影在线 | 国产免费观看一级国产 | 亚洲国产一区二区视频 | 91在线视频一区 |