发布MVC 4应用后,蔚蓝色的身份验证问题 [英] Authentication problems after publishing MVC 4 app to azure

查看:98
本文介绍了发布MVC 4应用后,蔚蓝色的身份验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我在Azure上的网站托管一个基本的ASP.NET MVC 4个网站。验证是形式的认证,并没有被从默认模板定制。我每次发布,当我重新审视我的网站,它只是终于露出了我一个错误消息之前很长的超时(两分钟,也许)挂起。我可以通过删除该网站的cookies在我的浏览器和重装恢复。

最初的问题,只是想访问需要身份验证的页面,后来我加入这个到我的共享 _Layout.cshtml

  @if(User.IsInRole(管理))
{
    <立GT; @ Html.ActionLink(管理,索引,管理)< /李>
}

这意味着现在根本没有任何页面都可以访问后新的发布,所以我甚至不能点击注销链接,这是另一种方式,我曾经是能够解决问题。

让我得到的东西配置错了吗?虽然我有一种变通方法,我可以用我自己,这会不会是该网站的用户提供了良好的经验,我发布一个更新后。

Edit:从ELMAH日志,好像窗体身份验证尝试时,我打电话IsInRole创建一个SQL防爆preSS数据库。我不明白为什么会这样做,因为我的窗体身份验证的所有设置为使用我的SQL Azure数据库。

  System.Web.HttpException(0X80004005):无法连接到SQL Server数据库。 ---> System.Data.SqlClient.SqlException(0x80131904):在与SQL Server建立连接时出现与网络相关的或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确,以及SQL Server配置为允许远程连接。 (提供者:SQL网络接口,错误:26  - 错误定位服务器/实例指定)
   在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection)
   在System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   在System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo,SqlInternalConnectionTds connHandler,布尔ignoreSniOpenTimeout,Int64的timerExpire,布尔值加密,布尔trustServerCert,布尔integratedSecurity布尔withFailover)
   在System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,字符串NEWPASSWORD,布尔ignoreSniOpenTimeout,TimeoutTimer超时,SqlConnection的owningObject,布尔withFailover)
   在System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo,字符串NEWPASSWORD,布尔redirectedUserInstance,SqlConnection的owningObject,SqlConnectionString connectionOptions,TimeoutTimer超时)
   在System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection的owningObject,TimeoutTimer超时,SqlConnectionString connectionOptions,字符串NEWPASSWORD,布尔redirectedUserInstance)
   在System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity身份,SqlConnectionString connectionOptions,对象providerInfo,字符串NEWPASSWORD,SqlConnection的owningObject,布尔redirectedUserInstance)
   在System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项,对象poolGroupProviderInfo,池类DBConnectionPool,的DbConnection owningConnection)
   在System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(的DbConnection owningConnection,DbConnectionPoolGroup poolGroup)
   在System.Data.ProviderBase.DbConnectionFactory.GetConnection(的DbConnection owningConnection)
   在System.Data.ProviderBase.DbConnectionClosed.OpenConnection(的DbConnection outerConnection,DbConnectionFactory connectionFactory的)
   在System.Data.SqlClient.SqlConnection.Open()
   在System.Web.Management.SqlServices.GetSqlConnection(字符串服务器,用户字符串,字符串密码,布尔信赖,字符串的connectionString)
ClientConnectionId:00000000-0000-0000-0000-000000000000
   在System.Web.Management.SqlServices.GetSqlConnection(字符串服务器,用户字符串,字符串密码,布尔信赖,字符串的connectionString)
   在System.Web.Management.SqlServices.SetupApplicationServices(字符串服务器,用户字符串,字符串密码,布尔信赖,字符串的connectionString,数据库字符串,字符串dbFileName,SqlFeatures功能,布尔安装)
   在System.Web.Management.SqlServices.Install(数据库字符串,字符串dbFileName,字符串的connectionString)
   在System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(字符串fullFileName,DATADIR字符串,字符串的connectionString)
   在System.Web.DataAccess.SqlConnectionHelper.EnsureSqlEx pressDBFile(字符串的connectionString)
   在System.Web.DataAccess.SqlConnectionHelper.GetConnection(字符串的connectionString,布尔revertImpersonation)
   在System.Web.Security.SqlRoleProvider.GetRolesForUser(字符串的用户名)
   在WebMatrix.WebData.SimpleRoleProvider.GetRolesForUser(字符串的用户名)
   在System.Web.Security.RolePrincipal.IsInRole(字符串角色)


解决方案

试图几十各种博客文章不同的建议后,我已经找到了解决方案。添加 InitialiseSimpleMembership 属性来我家的控制器解决该问题。

  [InitializeSimpleMembership]
公共类HomeController的:控制器

进行此更改后,我成功了几次成功的出版没有问题。我怀疑的原因是,在 InitializeSimpleMembershipAttribute 构造code的下面一行需要 User.IsInRole

  WebSecurity.InitializeDatabaseConnection(DefaultConnection,用户配置,用户ID,用户名,autoCreateTables:真正的);

我想做到最好的事情是运行 InitializeSimpleMembership 的Application_Start

I have a basic ASP.NET MVC 4 site which I am hosting on Azure websites. The authentication is forms authentication and has not been customised from the default template. Every time I publish, when I revisit my site, it just hangs with a very long timeout (couple of minutes perhaps) before finally showing me an error message. I can recover by deleting the cookies for the site in my browser and reloading.

Initially the problem was just trying to access pages that required authentication, but then I added this to my shared _Layout.cshtml:

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

which now means no pages at all are accessible after an new publish, and so I can't even click the logout link, which was another way I used to be able to fix the issue.

Have I got something configured wrong? Although I have a workaround I can use myself, this will not be a good experience for users of the site after I publish an update.

EDIT: from the ELMAH logs, it seems like forms authentication is trying to create a SQL Express database when I call IsInRole. I can't see why it would do this, as my forms authentication is all set up to use my SQL Azure database.

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, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject, Boolean withFailover)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, 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 user, String password, Boolean trusted, String connectionString)
   at System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install)
   at System.Web.Management.SqlServices.Install(String database, String dbFileName, 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)

解决方案

After trying dozens of different suggestions from various blog posts, I have found a solution. Adding the InitialiseSimpleMembership attribute to my home controller resolves the problem.

[InitializeSimpleMembership]
public class HomeController : Controller

After making this change, I managed several successful publishes without issues. I suspect the reason is that the following line of code in the InitializeSimpleMembershipAttribute constructor needs to run before any calls to User.IsInRole are made:

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

I guess the best thing to do would be to run InitializeSimpleMembership in Application_Start.

这篇关于发布MVC 4应用后,蔚蓝色的身份验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆