SimpleMembership,MVC4,AuthorizeAttribute和Roles

Dyl*_*rry 11 asp.net-mvc-4 simplemembership

我想补充授权属性,我的一些MVC4控制器,他们罚款,只要它是一个计划工作[Authorize][Authorize(Users="myuser")],但第二次我在任何类型的过滤作用,它的加入分崩离析,例如.[Authorize(Roles="admin")].然后我开始得到如下错误:

'/'应用程序中的服务器错误.

建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确,以及SQL Server是否配置为允许远程连接.(提供程序:SQL网络接口,错误:26 - 查找指定的服务器/实例时出错)

描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

SQLExpress数据库文件自动创建错误:

连接字符串使用应用程序的App_Data目录中的数据库位置指定本地Sql Server Express实例.提供程序尝试自动创建应用程序服务数据库,因为提供程序确定数据库不存在.要成功检查应用程序服务数据库是否存在并自动创建应用程序服务数据库,必须满足以下配置要求:

如果应用程序在Windows 7或Windows Server 2008R2上运行,则需要执行特殊配置步骤才能自动创建提供程序数据库.有关其他信息,请访问:http://go.microsoft.com/fwlink/?LinkId = 160102.如果应用程序的App_Data目录尚不存在,则Web服务器帐户必须具有对应用程序目录的读写权限.这是必要的,因为Web服务器帐户将自动创建App_Data目录(如果它尚不存在).如果应用程序的App_Data目录已存在,则Web服务器帐户仅需要对应用程序的App_Data目录的读写访问权限.这是必要的,因为Web服务器帐户将尝试验证应用程序的App_Data目录中是否已存在Sql Server Express数据库.从Web服务器帐户撤消对App_Data目录的读取访问将阻止提供程序正确确定Sql Server Express数据库是否已存在.当提供程序尝试创建现有数据库的副本时,这将导致错误.需要写访问权限,因为在创建新数据库时会使用Web服务器帐户的凭据.必须在计算机上安装Sql Server Express.Web服务器帐户的进程标识必须具有本地用户配置文件.有关如何为计算机和域帐户创建本地用户配置文件的详细信息,请参阅自述文档.

来源错误:

在执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息.

堆栈跟踪:

[SqlException(0x80131904):建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称是否正确,以及SQL Server是否配置为允许远程连接.(提供程序:SQL网络接口,错误:26 - 错误定位指定的服务器/实例)]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection,操作1重试,DbConnectionOptions userOptions,DbConnectionInternal&connection)+5311874 System.Data .ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory,TaskCompletionSource 1 retry)+83 System.Data.SqlClient.SqlConnection.Open()+96 System.Web.Management.SqlServices.GetSqlConnection(String server,String user,String password) ,布尔值信任,String connectionString)+761 wrapCloseInAction) +5295167 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242
System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5307115
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +920
System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions) +434
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +5309659
System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) +38
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource

1 retry, DbConnectionOptions userOptions) +143
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource

[HttpException(0x80004005):无法连接到SQL Server数据库.]
System.Web.Management.SqlServices.GetSqlConnection(String server,String user,String password,Boolean trusted,String connectionString)+131
System.Web.Management.SqlServices. SetupApplicationServices(String server,String user,String password,Boolean trusted,String connectionString,String database,String dbFileName,SqlFeatures features,Boolean install)+89
System.Web.Management.SqlServices.Install(String database,String dbFileName,String connectionString) +27
System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName,String dataDir,String connectionString)+386

版本信息:Microsoft .NET Framework版本:4.0.30319; ASP.NET版本:4.0.30319.17929

我真的不确定这里发生了什么.同样,如果我删除authorize属性的角色位,那么它工作正常(至少阻止未经授权的用户)但是如果我必须将管理员用户硬编码到应用程序中则没有用!

知道这里发生了什么,以及如何让它正常工作?

谢谢,

Dyl*_*rry 15

我找到了解决方案(虽然我确信有更好的方法).首先,SimpleMembership使用的数据库没有及早初始化,所以我移动了这一行:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
Run Code Online (Sandbox Code Playgroud)

到Global.asax文件,并确保它尽早运行.代码行可以在第41行的Filters - > InitializeSimpleMembershipAttribute.cs中找到

编辑:似乎下一点不是必要的......

其次,[InitializeSimpleMembership]属性需要添加到任何可能成为问题的类中 - 对我来说就是全部,所以我添加了这一行:

filters.Add(new InitializeSimpleMembershipAttribute());
Run Code Online (Sandbox Code Playgroud)

到app_start文件夹中的FilterConfig.cs文件.

这两个简单的变化似乎已经解决了.我确信我可以对MVC4中包含的SimpleMembership内容的一般工作做一些改进 - 虽然它看起来不是很好(硬编码连接字符串等!),所以可能是这些问题可能是通过将SimpleMembership的东西写得很好来修复!


mos*_*ers 6

你不应该移动第一行.我通过添加过滤器进行测试

filters.Add(new InitializeSimpleMembershipAttribute());
Run Code Online (Sandbox Code Playgroud)

虽然如果您要创建一个要求您登录的站点,您可以将其放在该控制器上,而不必将其放在所有控制器上.