2

我正在为我的 web api 使用基本身份验证,并收到以下错误

*{"Message":"发生错误。","ExceptionMessage":"建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。找不到或无法访问服务器。验证实例名称正确,并且 SQL Server 配置为允许远程连接。(提供者:命名管道提供者,错误:40 - 无法打开与 SQL 的连接 *

当我从我的 API 中删除基本身份验证实现时,它工作得非常好。所以我猜测问题应该与用于基本身份验证实现的 system.web.providers 有关。

这就是我的连接字符串的样子

<connectionStrings>
<add name="DefaultConnection" connectionString="Password=password;Persist Security Info=True;User ID=wbuser;Initial Catalog=WinBouts_com;Data Source=WINBOUTSAPIVM"
  providerName="System.Data.SqlClient" />
<add name="WinBoutsConnectionString" connectionString="Password=password;Persist Security Info=True;User ID=wbuser;Initial Catalog=WinBouts_com;Data Source=WINBOUTSAPIVM"
  providerName="System.Data.SqlClient" />    

我在我的 web.config 中看到一个错误,其中添加了会话提供程序。错误显示“不允许使用 connectionStringName 属性”。但如果我删除它,它会要求连接字符串名称。这是它的代码。

 <!--
        If you are deploying to a cloud environment that has multiple web server instances,
        you should change session state mode from "InProc" to "Custom". In addition,
        change the connection string named "DefaultConnection" to connect to an instance
        of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
  -->
<sessionState mode="Custom" customProvider="DefaultSessionProvider">
  <providers>
    <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="WinBoutsConnectionString" />
  </providers>
</sessionState>

谁能告诉我哪里可能出错了?任何建议或想法将不胜感激。

4

2 回答 2

2

终于解决了问题。问题出在我的基本身份验证过滤器类中。

private bool TryGetPrincipal(string username, string password, out IPrincipal principal)
    {      string connectionString = ConfigurationManager.ConnectionStrings["WinBoutsConnectionString"].ConnectionString;    

        username = username.Trim();
        password = password.Trim();

        //only supporting SSO login atm
       // var valid = Membership.Provider.ValidateUser(username, password);
        serviceContext = new WinBoutsDataContext<DataAccess.Model.UserInfo>(connectionString);
        userRepository = new UserRepository(serviceContext);

        var valid = this.userRepository.ValidateAccount(username, password);

        if (valid)
        {
            // once the user is verified, assign it to an IPrincipal with the identity name and applicable roles
            principal = new GenericPrincipal(new GenericIdentity(username), System.Web.Security.Roles.GetRolesForUser(username));
            return true;
        }

        //user wasn't found, unauthorised
        principal = null;
        return false;
    }

我在这里对我的 DataContext 进行了硬编码,但之前我没有将连接字符串传递给它。我只是选择了 WinBoutsDataContext 的默认构造函数,当我将代码放到服务器上时它不起作用。

于 2012-11-27T08:41:52.980 回答
1

如果要使用已定义的连接字符串,则必须使用连接字符串的名称来填充“sqlConnectionString”属性并使用SqlServer模式:

<configuration>
  <connectionStrings>
    <add name = "myString"
         connectionString = "data source=local;user id=User;password=password" />
  </connectionStrings>
  <system.web>
    <sessionState
      mode="SQLServer"
      sqlConnectionString="myString"
      cookieless="false"
      timeout="20"
    />
  </system.web>
</configuration>
于 2014-09-16T09:34:13.400 回答