在sessionstate connectionstring

时间:2015-08-28 15:36:34

标签: c# asp.net session-state

我正在开发一个捐赠给我们机构的网站,现在我正在尝试确定如何在Web.config文件中设置两个连接字符串(CS)(这些连接字符串中的一些细节)由捐赠者编辑)。 CS我有一个问题在sessionState中被引用:

<sessionState allowCustomSqlDatabase="true" mode="SQLServer" sqlConnectionString="DevelopmentSessionState" cookieless="false" timeout="20"/>

我正在尝试确定我应该在SessionState CS中为UserID使用哪种类型的帐户。为了获得网站功能,我使用了sa帐户,但我知道这不是一个好主意 - 我正在寻找一个帐户来取代sa。我不知道这是否是我应该在SQL Server中定义的用户,或者我是否应该使用预先存在的帐户,或者该用户应该具有哪种类型的访问权限。

我对于为什么会话状态需要单独的连接字符串的想法也很模糊。网站的c#代码显式使用其他CS连接到数据库,但对SessionState CS的唯一引用是在Web.config中。过去几天我已经阅读了很多关于会话状态和SQLServer模式的内容,但是我没有找到任何关于使用什么帐户的建议,或者为什么SessionState需要单独的CS。

谢谢, 克里斯

更新:这是当我在会话状态CS中用我的UserID替换sa时发生的异常终止的堆栈跟踪(我用USERID替换了我的实际用户ID):

Server Error in '/' Application.

Failed to login to session state SQL server for user 'USERID'. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: Failed to login to session state SQL server for user 'USERID'.

Source Error: 


An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 



[SqlException (0x80131904): Login failed for user 'USERID'.]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +4876535
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +194
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) +1121
System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK) +35
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject) +144
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart) +367
System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +225
System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +189
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +185
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options) +31
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject) +431
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject) +66
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +499
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +65
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +117
System.Data.SqlClient.SqlConnection.Open() +122
System.Web.SessionState.SqlStateConnection..ctor(SqlPartitionInfo sqlPartitionInfo) +93

[HttpException (0x80004005): Failed to login to session state SQL server for user 'USERID'.]




Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5491  

2 个答案:

答案 0 :(得分:1)

我最终成功创建了一个在Session State CS中工作的用户。

首先,我运行了aspnet_regsql.exe,而不是运行InstallSqlState.sql。 SQL文件有一个警告,不要独立运行它,我应该使用aspnet_regsql.exe,所以我这样做了。我使用了以下选项:

aspnet_regsql.exe -ssadd -sstype p -E -S servername

这创建了db ASPState,这意味着我必须在web.config中使用mode =“SQLServer”。

我还必须按照此处的建议将角色'public'添加到ASPState: http://forums.asp.net/t/969708.aspx 这篇文章包括授予“公共”权限,但没有谈论需要哪些权限,因此我必须在CS工作之前进行实验。通过右键单击db ASPState,选择Properties,选择Permissions,突出显示数据库角色'public',然后在'Grant'标题下检查权限来添加权限。

最后,我使用SQL Server身份验证登录到SQL Server Management Studio并创建了一个新用户S1。我只使用上述相同的导航向S1授予“连接”权限。 S1在会话状态连接字符串中替换了sa。这是会话状态CS的web.config条目。显然我改变了pw和servername,但你可以看到我使用的格式:

<add name="DVSessionState" connectionString="Data Source=servername;User Id='S1';Password='password'"/>

答案 1 :(得分:0)

以下步骤描述了如何运行InstallSqlState.sql和UninstallSqlState.sql脚本文件以配置SQL Server模式会话状态管理。

在SQL查询分析器的“文件”菜单上,单击“打开”。 在“打开查询文件”对话框中,浏览到InstallSqlState.sql脚本文件,然后单击“打开”。默认情况下,InstallSqlState.sql位于以下文件夹之一:

system drive\WINNT\Microsoft.NET\Framework\version\
system drive\Windows\Microsoft.NET\Framework\version\

在SQL查询分析器中打开InstallSqlState.sql后,单击“查询”菜单上的“执行”以运行脚本。

在运行UninstallSqlState.sql脚本文件以卸载SQL Server模式会话状态管理配置之前,必须停止w3svc进程。为此,请按照下列步骤操作:

  1. 在Windows“开始”菜单上,单击“运行”,键入cmd,然后单击“确定”以打开命令提示符。
  2. 在命令提示符下,键入net stop w3svc。您收到w3svc进程停止的确认。
  3. 在SQL查询分析器的“文件”菜单上,单击“打开”。
  4. 在“打开查询文件”对话框中,浏览到UninstallSqlState.sql脚本文件,然后单击“打开”。默认情况下,UninstallSqlState.sql位于以下文件夹之一:

    系统驱动\ WINNT \ Microsoft.NET \ Framework \ version \ 系统驱动\ Windows \ Microsoft.NET \ Framework \ version \

  5. 在SQL查询分析器中打开UninstallSqlState.sql后,单击“查询”菜单上的“执行”以运行脚本。

  6. 卸载SQL Server模式会话状态管理配置后,必须重新启动w3svc服务。要重新启动w3svc进程,请在命令提示符下键入net start w3svc
  7. 修改应用程序的Web.config文件

    要实现ASP.NET SQL Server模式会话状态管理,您必须修改应用程序的Web.config文件的<sessionState>元素,如下所示:

    1. <sessionState>元素的mode属性设置为SQLServer,以指示会话状态存储在SQL Server中。
    2. 设置sqlConnectionString属性以指定SQL Server的连接字符串。例如:

      sqlConnectionString =&#34;数据源= MySQLServer;用户ID =;密码=&#34;

    3. 注意:用户<user name>必须具有在数据库上执行此操作的权限。

      修改后的<sessionState>元素应如下所示:

      <sessionState 
                  mode="SQLServer"
                  sqlConnectionString="data source=127.0.0.1;user id=<username>;password=<strongpassword>"
                  cookieless="false" 
                  timeout="20" 
          />
      

      注意:确保在指定<sessionState>元素和关联的属性值时使用正确的大小写。此代码区分大小写。