11

我正在尝试向我的一些 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 服务器帐户必须具有对应用程序目录的读写权限。这是必要的,因为如果 App_Data 目录不存在,Web 服务器帐户将自动创建它。如果应用程序的 App_Data 目录已经存在,则 Web 服务器帐户只需要对应用程序的 App_Data 目录具有读写权限。这是必要的,因为 Web 服务器帐户将尝试验证 Sql Server Express 数据库是否已存在于应用程序的 App_Data 目录中。从 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& 连接)+5311874 System.Data .ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 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
1 次重试) +83 System.Data.SqlClient.SqlConnection.Open() +96
System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trust, String connectionString) +76

[HttpException (0x80004005): 无法连接到 SQL Server 数据库。]
System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trust, String connectionString) +131
System.Web.Management.SqlServices。 SetupApplicationServices(字符串服务器,字符串用户,字符串密码,布尔信任,字符串连接字符串,字符串数据库,字符串 dbFileName,SqlFeatures 功能,布尔安装)+89
System.Web.Management.SqlServices.Install(字符串数据库,字符串 dbFileName,字符串连接字符串) +27
System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(字符串 fullFileName,字符串 dataDir,字符串 connectionString)+386

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

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

知道这里发生了什么,以及如何使其正常工作吗?

谢谢,

4

5 回答 5

15
于 2012-09-09T20:07:55.300 回答
6

您不需要移动第一行。我通过添加过滤器进行了测试

filters.Add(new InitializeSimpleMembershipAttribute());

虽然如果您正在创建一个需要您登录的站点,您可以将它放在那个控制器上,而不必将它放在所有控制器上。

于 2012-09-13T00:48:51.490 回答
2

由 MVC 4 的 Internet 模板生成的 InitializeSimpleMembershipAttrribute 设计用于在开发人员未使用表单身份验证的情况下延迟加载 SimpleMembership 数据库。如果您使用表单身份验证,建议删除此过滤器并直接对其进行初始化。本文介绍了如何执行此操作。

于 2013-03-06T14:31:25.527 回答
2

在 MVC4 中,我能够将注释添加到我的家庭控制器中,并且此错误停止出现:

[InitializeSimpleMembership]

还必须根据需要在顶部引入进口:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.AspNet;
using Microsoft.Web.WebPages.OAuth;
using WebMatrix.WebData;
于 2013-07-28T14:32:39.097 回答
1

检查InitializeSimpleMembershipAttributeSimpleProviders 的使用情况、web.config 设置,还可以考虑InitializeSimpleMembershipAttribute在使用时专门制作授权过滤器Authorize(Roles="any")

于 2013-10-15T20:37:33.107 回答