【问题标题】:What account to use in sessionstate connectionstring在会话状态连接字符串中使用什么帐户
【发布时间】:2015-08-28 15:36:34
【问题描述】:

我正在开发一个捐赠给我们机构的网站,现在我正在尝试确定如何在 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 中。在过去的几天里,我阅读了很多关于 Session State 和 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  

【问题讨论】:

  • 通常您可以在多个应用程序之间共享 sessionState 数据库,因此拥有一个帐户是一种公认​​的做法。无论您选择使用 SQL 还是集成 Windows 身份验证都是您应该权衡组织的整体战略的设计选择。例如,某些环境更喜欢使用 Windows Auth 以避免将系统密码交到开发人员手中。
  • 感谢 mjw - 这很有帮助。我现在应该可以选择合适的帐户了。

标签: c# asp.net session-state


【解决方案1】:

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

首先,我没有运行 InstallSqlState.sql,而是运行了 aspnet_regsql.exe。 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,选择属性,选择权限,突出显示数据库角色“公共”,然后在“授予”标题下检查权限来添加权限。

最后我使用 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'"/>

【讨论】:

    【解决方案2】:

    以下步骤描述了如何运行 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\
    

    InstallSqlState.sql 在 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. UninstallSqlState.sql 在 SQL 查询分析器中打开后,单击查询菜单上的执行以运行脚本。

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

    修改您的应用程序的 Web.config 文件

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

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

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

    注意:用户&lt;user name&gt; 必须具有对数据库执行此操作的权限。

    修改后的&lt;sessionState&gt;元素应如下所示:

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

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

    【讨论】:

    • 感谢大家提供的有用信息 - 我以为我已经解决了这个问题,但除了 sa 之外,我找不到任何用户可以在此连接字符串中工作。我认为我的问题是不知道用户必须拥有什么权限。 Mohammed,我知道您说用户必须有权执行此操作,但我不知道“此操作”是什么意思...连接到数据库?这些权限是否在 SQL Server Management Studio 中定义?
    猜你喜欢
    • 2011-09-19
    • 2016-06-02
    • 2023-04-07
    • 2013-04-09
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    相关资源
    最近更新 更多