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

Entity Framework+Xamarin Forms集成身份验证登录失败问题求助

错误分析与解决:Login failed. The login is from an untrusted domain and cannot be used with Integrated authentication.

问题描述

在基于Entity Framework的Xamarin Forms项目中执行数据库操作时,触发以下身份验证错误:
{"Login failed. The login is from an untrusted domain and cannot be used with Integrated authentication."}

相关代码片段:

public int Find(string AccountNumber, DateTime DepositedDT)
{
    IsPigmySync pigmySync = new IsPigmySync();
    pigmySync.AccountNumber = AccountNumber;
    pigmySync.DepositedDT = DepositedDT;

    SqlParameter issynced = new SqlParameter("@p2", System.Data.SqlDbType.Int);
    issynced.Direction = ParameterDirection.Output;

    try
    {
        var sql = "exec Pigmy_GetPigmyItems @p0,@p1,@p2 OUT";
        
        // var result = _context.Database.ExecuteSqlInterpolated(sqlQuery);
        var result = _context.Database.ExecuteSqlCommand(sql, pigmySync.AccountNumber, pigmySync.DepositedDT, issynced);
        int ab = result;
        pigmySync.IsSynced = (int)issynced.Value;

        return pigmySync.IsSynced;
    }
    catch (Exception ex)
    {
        return 0;
    }
}

错误原因

核心问题出在数据库连接字符串的身份验证方式:
如果你的连接字符串使用了Integrated Security=True(集成Windows身份验证),Xamarin Forms作为跨平台移动应用,运行在安卓/iOS设备上时,这些设备并不属于你的Windows域环境,无法通过当前设备的Windows账户凭证完成SQL Server的身份验证,因此触发该错误。代码本身的存储过程调用逻辑没有问题,问题根源在连接配置。

解决建议

  • 改用SQL Server身份验证:修改连接字符串,将集成身份验证替换为SQL Server账号密码验证。示例:
    Server=你的SQL服务器地址;Database=你的数据库名;User ID=SQL账号;Password=SQL密码;
    
    同时要确保SQL Server已开启混合身份验证模式(SQL Server和Windows身份验证模式)。
  • 检查SQL Server远程连接配置:确认SQL Server允许远程连接,并且SQL Server配置管理器中已启用TCP/IP协议。
  • 验证网络连通性:确保移动设备能访问SQL Server所在的网络(如同一局域网、VPN连接),网络不通也会间接导致身份验证失败。
  • 确认连接字符串配置正确:检查EF上下文的连接字符串是否正确配置(如在App.xaml.cs或配置文件中),避免硬编码时出现拼写错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 12:00:51