C# · 12月 29, 2021

c# – 将MVC 4应用程序发布到天蓝色后的身份验证问题

我有一个基本的ASP.NET MVC 4网站,我在Azure网站上托管.身份验证是表单身份验证,还没有从默认模板中进行自定义.每次发布时,当我重新访问我的网站时,它会挂起很长的超时时间(几分钟),然后再显示一条错误信息.我可以通过删除我的浏览器中的网站的cookie并重新加载来恢复.

最初,问题只是尝试访问需要身份验证的页面,但是我将其添加到我的共享_Layout.cshtml中:

@if (User.IsInRole(“Admin”)){ <li>@Html.ActionLink(“Admin”,”Index”,”Admin”)</li>}

现在这意味着在新的发布之后,所有的页面都不可访问,所以我甚至不能单击退出链接,这是我以前能够解决问题的另一种方式.

有没有配置错误?虽然我有一个解决方法我可以使用自己,这不会是一个很好的经验,网站的用户发布更新后.

编辑:从ELMAH日志,当我调用IsInRole,似乎窗体身份验证正在尝试创建一个sql Express数据库.我不明白为什么会这样做,因为我的表单身份验证全部设置为使用我的sql Azure数据库.

System.Web.HttpException (0x80004005): Unable to connect to sql Server database. —> System.Data.sqlClient.sqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to sql Server. The server was not found or was not accessible. Verify that the instance name is correct and that sql Server is configured to allow remote connections. (provider: sql Network Interfaces,error: 26 – Error Locating Server/Instance Specified) at System.Data.sqlClient.sqlInternalConnection.OnError(sqlException exception,Boolean breakConnection) at System.Data.sqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.sqlClient.TdsParser.Connect(ServerInfo serverInfo,sqlInternalConnectionTds connHandler,Boolean ignoreSniOpenTimeout,Int64 timerExpire,Boolean encrypt,Boolean trustServerCert,Boolean integratedSecurity,Boolean withFailover) at System.Data.sqlClient.sqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,String newPassword,TimeoutTimer timeout,sqlConnection owningObject,Boolean withFailover) at System.Data.sqlClient.sqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo,Boolean redirectedUserInstance,sqlConnectionString connectionOptions,TimeoutTimer timeout) at System.Data.sqlClient.sqlInternalConnectionTds.OpenLoginEnlist(sqlConnection owningObject,Boolean redirectedUserInstance) at System.Data.sqlClient.sqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity,Object providerInfo,Boolean redirectedUserInstance) at System.Data.sqlClient.sqlConnectionFactory.CreateConnection(DbConnectionOptions options,Object poolGroupProviderInfo,DbConnectionPool pool,DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection,DbConnectionPoolGroup poolGroup) at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory) at System.Data.sqlClient.sqlConnection.open() at System.Web.Management.sqlServices.GetsqlConnection(String server,String user,String password,Boolean trusted,String connectionString)ClientConnectionId:00000000-0000-0000-0000-000000000000 at System.Web.Management.sqlServices.GetsqlConnection(String server,String connectionString) at System.Web.Management.sqlServices.SetupApplicationServices(String server,String connectionString,String database,String dbFileName,sqlFeatures features,Boolean install) at System.Web.Management.sqlServices.Install(String database,String connectionString) at System.Web.DataAccess.sqlConnectionHelper.CreateMdfFile(String fullFileName,String dataDir,String connectionString) at System.Web.DataAccess.sqlConnectionHelper.EnsuresqlExpressDBFile(String connectionString) at System.Web.DataAccess.sqlConnectionHelper.GetConnection(String connectionString,Boolean revertImpersonation) at System.Web.Security.sqlRoleProvider.GetRolesForUser(String username) at WebMatrix.WebData.SimpleRoleProvider.GetRolesForUser(String username) at System.Web.Security.RolePrincipal.IsInRole(String role)解决方法 在尝试了各种博客文章中的数十种不同建议后,我找到了一个解决方案.将InitialiseSimpleMembership属性添加到我的家庭控制器可以解决问题. [InitializeSimpleMembership]public class HomeController : Controller

做了这个改变后,我管理了几个成功的发布没有问题.我怀疑原因是在对User.IsInRole进行任何调用之前,InitializeSimpleMembershipAttribute构造函数中的以下代码行需要运行:

WebSecurity.InitializeDatabaseConnection(“DefaultConnection”,”UserProfile”,”UserId”,”UserName”,autoCreateTables: true);

我想最好的办法是在Application_Start中运行InitializeSimpleMembership.