在会话状态connectionstring中使用什么帐户

本文关键字:什么 会话状态 connectionstring | 更新日期: 2023-09-27 18:10:19

我正在制作一个捐赠给我们机构的网站,现在我正试图决定如何在网络上设置两个连接字符串(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中。在过去的几天里,我读了很多关于SessionState和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  

在会话状态connectionstring中使用什么帐户

我最终成功地创建了一个在会话状态CS中工作的用户。

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

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

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

我还必须在ASPState中添加角色"public",如下所示:http://forums.asp.net/t/969708.aspx这篇文章包括给予"公共"权限,但没有讨论哪些是需要的,所以我不得不尝试这种方法,直到CS起作用。通过右键单击db ASPState,选择属性,选择权限,突出显示数据库角色'public',然后在'Grant'标题下勾选权限来添加权限。

最后我使用SQL Server身份验证登录到SQL Server Management Studio,并创建了一个新用户S1。我只使用上述相同的导航向S1授予了"连接"权限。日志含义会话状态连接字符串中的sa替换为S1。这是网络。会话状态CS的配置项。显然,我更改了pw和服务器名,但您可以看到我使用的格式:

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

下面介绍如何运行InstallSqlState。卸载sqlstate . sql和卸载sqlstate . 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 '微软。净' ' Framework '版本系统驱动' Windows '微软。净' Framework ' '版

  5. 在SQL查询分析器中打开UninstallSqlState.sql后,在"查询"菜单中单击"执行",运行脚本

  6. 卸载SQL Server模式会话状态管理配置后,必须重新启动w3svc服务。要重新启动w3svc进程,在命令提示符下输入net start w3svc

修改Web。应用程序配置文件

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

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

    sqlConnectionString="data source=MySQLServer;user id=;password="

注意:用户<user name>必须具有对数据库执行该操作的权限。

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

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

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