繁体   English   中英

Asp.net 会员提供程序无法连接到内部部署 SQL 服务器部署到 Azure 应用程序服务

[英]Asp.net Membership provider fails to connect to on premise SQL Server when deployed to Azure app service

我正在尝试将我的 asp.net MVC (V 4.0/.Net Framework 4.5) 之一移植到 Azure 作为应用程序服务。 我的数据库是 SQL Server,我的应用程序使用Hybrid Connection连接到它。 我的应用程序使用 SQL 身份验证连接到数据库。 当部署到我们本地托管服务器之一上的 IIS 服务器时,一切正常,但当作为应用服务部署到 Azure 时,身份验证功能会中断。

我的 controller 代码:

[HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public virtual ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                //var v = Membership.GetAllUsers();
                try
                {
                    if (Membership.ValidateUser(model.UserName, model.Password))
                    {
                        FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                        if (Url.IsLocalUrl(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index", "Home");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("", "The user name or password provided is incorrect.");
                    }
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", ex.ToString());
                }
                
            }

Azure 报错:

System.Data.SqlClient.SqlException (0x80131904):用户“OSUser”登录失败。 at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager )在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions 选项,DbConnectionPoolKey poolKey,Object poolGroupProviderInfo,DbConnectionPool 池,DbConnection owningConnection,DbConnectionOptions userOptions) DbConnectionPoolKey poolKey,DbConnectionOptions userOptions)在 System.Data.ProviderBase.DbConnectionPool。 在 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject,DbConnectionOptions userOptions,DbConnectionInternal oldConnection)在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,UInt32 waitForMultiple8117 允许 16,18 ,Boolean onlyOneCheckConnection,DbConnectionOptions userOptions,DbConnectionInternal&连接)在System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource重试,DbConnectionOptions userOptions,DbConnectionInternal oldConnection,DbConnectionInternal& 连接)在 System.Data.ProviderBase.DbConnectionInternal.TryOpen ConnectionInternal(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource 1 retry, DbConnectionOptions userOptions) at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1 重试,DbConnectionOptions userOptions)在 System.Data.SqlClient.SqlOpenInConnection(。 TaskCompletionSource 1 retry) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1 重试)在 System.Data.SqlClient.SqlConnection.Open() 在 System.Web.DataAccess.SqlConnectionHolder.Open(HttpContext 上下文,Boolean revertImpersonate)在系统.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString, Boolean revertImpersonation) at System.Web.Security.SqlMembershipProvider.GetPasswordWithFormat(String username, Boolean updateLastLoginActivityDate, Int32& status, String& password, Int32& passwordFormat, String& passwordSalt, Int32& failedPasswordAttemptCount, Int32& failedPasswordAnswerAttemptCount, Boolean& isApproved, DateTime& lastLoginDate, DateTime& lastActivityDate) at System.Web.Security.SqlMembershipProvider.CheckPassword(String username, String password, Boolean updateLastLoginActivityDate, Boolean failIfNotApproved, String& salt, Int32& passwordFormat) at System.Web .Security.SqlMembershipProvider.ValidateUser(String username, String password) 在 WebMatrix.WebData.SimpleMembershipProvider.ValidateUser(String username, String password) 在 USC.Controllers.AccountController.Login(LoginModel model, String returnUrl) 在 D:\a\1 \s\USC\Controllers\AccountController.cs:第 36 行 ClientConnectionId:b1f6b763-80f6-47f0-9a50-c70ab30635bd 错误号:18456,State:1,Class:14

有任何想法吗??? 谢谢!

消息“用户登录失败”是一条一般错误消息,可能有多种原因。 出于安全原因,发送给客户端的错误消息不包含有关登录失败原因的信息。

您需要授予此用户登录 SQL 服务器的权限以及访问数据库的权限: 创建登录 - SQL 服务器

您连接的数据库可能是在您部署应用程序时使用旧版本的 .NET Framework 的 as.net regsql.exe 工具生成的。 尝试使用您在本地使用的相同版本的 as.net regsql 来创建和配置成员数据库。 也许您只是在本地使用了 SQL Express 和 /App Data 文件夹。

在两个数据库中,比较表结构。 请比较 AspNet 成员表。

您甚至可以参考:System.Data.SqlClient.SqlException: 用户登录失败 | ASP.NET论坛

疑难解答:用户“x”登录失败 | 微软文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM