You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure SQL托管实例能否强制含@的包含用户用SQL身份验证登录?

问题:Azure SQL托管实例中强制带@其他租户后缀的包含数据库用户使用SQL Server身份验证登录

环境信息

  • 迁移场景:从SQL Server 2022迁移至Azure SQL托管实例
  • 托管实例租户:@contoso.com
  • 实例名称:sqltest
  • 目标数据库:MyTestDB
  • 身份验证模式:混合模式(SQL Server身份验证 + Entra ID登录)

问题现象

管理员账户、Entra账户及多数SQL Server身份验证账户均可正常登录,但名为user@other.com的包含数据库用户无法通过SQL Server身份验证登录。该用户创建语句如下:

CREATE USER [user@other.com] 
    WITH PASSWORD=N'...', DEFAULT_SCHEMA=[dbo]
GO

已尝试操作及结果

  • 尝试修改用户名中的@为其他字符,但会影响现有依赖,不可行
  • 尝试在用户名后添加@servername(如user@other.com@sqltest),登录失败,错误日志显示:

Login failed for user 'user@other.com@sqltest'. Reason: Could not find a login matching the name provided. [CLIENT: 172.16.0.5]
Error: 18456, Severity: 14, State: 5.

  • 设置connectionStringBuilder.Authentication = SqlAuthenticationMethod.SqlPassword;并将IntegratedSecurity设为false,仍无法强制SQL Server身份验证,登录时错误日志显示:
A disconnect event was raised when server is waiting for Federated Authentication token. This could be due to client close or server timeout expired.
Error: 33155, Severity: 20, State: 1.

该错误表明系统仍尝试通过其他租户的Entra ID进行身份验证。

核心疑问

在Azure SQL托管实例上,能否强制格式为user@othertenant.com的包含数据库用户通过SQL Server身份验证登录?

测试用C#代码

var connectionStringBuilder = new SqlConnectionStringBuilder();

connectionStringBuilder.DataSource = "sqltest.78ddsz94873.database.windows.net";
connectionStringBuilder.InitialCatalog = "MyTestDB";

#region test user
connectionStringBuilder.UserID = "user@other.com";
//connectionStringBuilder.UserID = "user@other.com@sqltest";
//connectionStringBuilder.UserID = "[sqltest\user@other.com]";
connectionStringBuilder.Password = "...";
connectionStringBuilder.Authentication = SqlAuthenticationMethod.SqlPassword;
connectionStringBuilder.IntegratedSecurity = false;
#endregion

connectionStringBuilder.ConnectRetryInterval = 1;
connectionStringBuilder.ConnectRetryCount = 1;
connectionStringBuilder.ConnectTimeout = 10;
connectionStringBuilder.TrustServerCertificate = true;

using var sqlConnection = new SqlConnection(connectionStringBuilder.ConnectionString);
sqlConnection.Open();
var cmd = sqlConnection.CreateCommand();

// Select @@ Version and log result
cmd.CommandText = "SELECT @@VERSION";
var reader = cmd.ExecuteReader();

while (reader.Read())
{
    Console.WriteLine(reader[0]);
}

reader.Close();
sqlConnection.Close();

Console.WriteLine("Press any key to continue");
Console.ReadKey();

解决方案

要强制这类带@后缀的包含数据库用户使用SQL Server身份验证,可按以下步骤处理:

1. 调整连接字符串中的用户名格式

在指定UserID时,用方括号包裹完整用户名,同时保留Authentication = SqlAuthenticationMethod.SqlPassword和IntegratedSecurity = false的设置,修改代码中的UserID配置:

connectionStringBuilder.UserID = "[user@other.com]";

此操作可避免Azure SQL托管实例将@other.com识别为Entra ID租户后缀,强制触发SQL Server身份验证流程。

2. 验证用户身份验证类型

在目标数据库中执行以下SQL,确认用户的身份验证类型为SQL Server身份验证:

USE MyTestDB;
SELECT name, type, authentication_type_desc FROM sys.database_users WHERE name = 'user@other.com';

需确保authentication_type_desc字段显示为INSTANCE。

3. 更新客户端驱动版本

确保使用的.NET SqlClient驱动为5.0及以上版本,旧版本可能存在身份验证逻辑兼容问题,无法正确解析特殊格式的用户名。


内容的提问来源于stack exchange,提问作者Reinard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 06:37:23