用户基于AD令牌的身份验证登录失败'&书信电报;令牌识别主体>';。在Azure SQL的实体框架6中

用户基于AD令牌的身份验证登录失败'&书信电报;令牌识别主体>';。在Azure SQL的实体框架6中,azure,entity-framework-6,azure-active-directory,azure-sql-database,Azure,Entity Framework 6,Azure Active Directory,Azure Sql Database,我已经为我的SQL数据库执行了Azure AD身份验证。 为此,我遵循以下步骤 我为门户中的SQL数据库设置了Azure AD管理员 列表项 获取身份验证令牌 private static string GetAccessTokenAsync(string clientId, string clientSecret, string authority, string resource, string scope) { var authContext = new Authentica

我已经为我的SQL数据库执行了Azure AD身份验证。 为此,我遵循以下步骤

  • 我为门户中的SQL数据库设置了Azure AD管理员

  • 列表项

  • 获取身份验证令牌

    private static string GetAccessTokenAsync(string clientId, string clientSecret, string authority, 
    string resource, string scope)
    {
        var authContext = new AuthenticationContext(authority, TokenCache.DefaultShared);
        var clientCred = new ClientCredential(clientId, clientSecret);
        var token = authContext.AcquireTokenAsync(resource, clientCred).Result.AccessToken;
    
    
        return token;
    }
    
  • sql连接失败了吗

        string clientId = ConfigurationManager.AppSettings["ida:AADClientId"];
        string clientSecret = ConfigurationManager.AppSettings["ida:AADAppKey"];
        var authority = string.Format("https://login.microsoftonline.com/{0}", tenantId);
        var resource = "https://database.windows.net/";
        var scope = "";
        try
        {
            var token = GetAccessTokenAsync(clientId, clientSecret, authority, resource, scope);
    
            var builder = new SqlConnectionStringBuilder();
            builder["Data Source"] = $"{dbServer}.database.windows.net";
            builder["Initial Catalog"] = dbName;
            builder["Connect Timeout"] = 1500;
            builder["Persist Security Info"] = false;
            builder["TrustServerCertificate"] = false;
            builder["Encrypt"] = true;
            builder["MultipleActiveResultSets"] = false;
    
            SqlConnection con = new SqlConnection(builder.ToString());
            con.AccessToken = token;
            return con;
        }
    
  • 数据库上下文类

     public partial class DBEntities : DbContext
        {
    //string dbConnectionString = 
          string.Concat(ConfigurationManager.AppSettings["subdbconnectionstring"], '"', 
    string.Format(ConfigurationManager.AppSettings["dbconnectionstring"], 
    ConfigurationManager.AppSettings["DBPassword"]),'"');
    
    //string test = ConfigurationManager.AppSettings["subdbconnectionstring"] + "\"" + ConfigurationManager.AppSettings["dbconnectionstring"];
    public DBEntities(SqlConnection con)
         : base(con, true)
    {
        {
            Database.SetInitializer<DBEntities>(null);
            ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 1800;
        }
     }
    
  • }

    现在我在连接到表时出错

    The underlying provider failed on Open.Login failed for user '<token-identified principal> 
    
    at System.Data.Entity.Core.EntityClient.EntityConnection.<OpenAsync>d__8.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Data.Entity.Core.Objects.ObjectContext.<EnsureConnectionAsync>d__9.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Data.Entity.Core.Objects.ObjectContext.<ExecuteInTransactionAsync>d__3d`1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 
      task)
    at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<ExecuteAsyncImplementation>d__9`1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
    at System.Data.Entity.Core.Objects.ObjectQuery`1.<GetResultsAsync>d__e.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
    at System.Data.Entity.Internal.LazyAsyncEnumerator`1.<FirstMoveNextAsync>d__0.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.<ForEachAsync>d__5`1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
    at CompanyDataService.Controllers.TeamController.<GetAllTeamRoles>d__0.MoveNext() in D:\sol\vs_project\DataService\DataService\Controllers\TeamController.cs:line 32
    
    基础提供程序在打开时失败。用户的登录失败
    在System.Data.Entity.Core.EntityClient.EntityConnection.d_u8.MoveNext()中
    ---来自引发异常的上一个位置的堆栈结束跟踪---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
    在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
    在System.Data.Entity.Core.Objects.ObjectContext.d_u9.MoveNext()中
    ---来自引发异常的上一个位置的堆栈结束跟踪---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
    在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
    位于System.Data.Entity.Core.Objects.ObjectContext.d_u3D`1.MoveNext()处
    ---来自引发异常的上一个位置的堆栈结束跟踪---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
    在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务
    (任务)
    在System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.d_u9`1.MoveNext()中
    ---来自引发异常的上一个位置的堆栈结束跟踪---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
    在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
    位于System.Data.Entity.Utilities.TaskExtensions.CultureAwater`1.GetResult()处
    在System.Data.Entity.Core.Objects.ObjectQuery`1.d_ue.MoveNext()中
    ---来自引发异常的上一个位置的堆栈结束跟踪---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
    在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
    位于System.Data.Entity.Utilities.TaskExtensions.CultureAwater`1.GetResult()处
    在System.Data.Entity.Internal.LazyAsyncEnumerator`1.d_u0.MoveNext()中
    ---来自引发异常的上一个位置的堆栈结束跟踪---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
    在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
    位于System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.d_u5`1.MoveNext()
    ---来自引发异常的上一个位置的堆栈结束跟踪---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)
    在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中
    在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()中
    位于d:\sol\vs\u project\DataService\DataService\Controllers\TeamController.d\uu 0.MoveNext()中的CompanyDataService.Controllers.TeamController.d\uu 0.MoveNext():第32行
    
    在您的步骤中,我没有看到您在azure ad中创建了应用程序注册,但您似乎在使用clientid和secret。你漏了一步吗?下面是如何使用服务主体连接到sql数据库的完整示例:


    关于,

    Hmm,看起来您正在作为应用程序进行身份验证。需要将此应用程序添加到SQL数据库中的角色。请参见此处:感谢Junnas的评论..是的,已从外部提供程序EXEC sp_addrolemember'db_owner',App1'添加到数据库中创建用户[App1],您好,此问题已解决吗?非常感谢,我收到错误:用户“”的登录失败,因为在Visual Studio中,访问令牌(jwt令牌)具有Visual Studio的应用程序id,而不是我的开发负责人的应用程序id。我遵循了你发布的链接中的教程,现在我的访问令牌具有我的开发负责人的应用程序id。
    The underlying provider failed on Open.Login failed for user '<token-identified principal> 
    
    at System.Data.Entity.Core.EntityClient.EntityConnection.<OpenAsync>d__8.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Data.Entity.Core.Objects.ObjectContext.<EnsureConnectionAsync>d__9.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Data.Entity.Core.Objects.ObjectContext.<ExecuteInTransactionAsync>d__3d`1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task 
      task)
    at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<ExecuteAsyncImplementation>d__9`1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
    at System.Data.Entity.Core.Objects.ObjectQuery`1.<GetResultsAsync>d__e.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
    at System.Data.Entity.Internal.LazyAsyncEnumerator`1.<FirstMoveNextAsync>d__0.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.<ForEachAsync>d__5`1.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
    at CompanyDataService.Controllers.TeamController.<GetAllTeamRoles>d__0.MoveNext() in D:\sol\vs_project\DataService\DataService\Controllers\TeamController.cs:line 32