我正在为我的网络API使用基本身份验证,并收到以下错误
* {“消息”:“发生错误。”,“ExceptionMessage”:“建立与SQL Server的连接时发生与网络相关或特定于实例的错误。未找到服务器或无法访问服务器。验证实例名称是否正确以及SQL Server是否配置为允许远程连接。(提供程序:命名管道提供程序,错误:40 - 无法打开与SQL *的连接
当我从API中删除基本身份验证实现时,它完全正常。所以我猜这个问题应该是与system.web.providers相关的东西,它们用于基本的auth实现。
这是我的连接字符串的样子
<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属性”。但是,如果我删除它然后它要求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>
任何人都可以告诉我哪里可能出错了吗?任何建议或想法都将深受赞赏。
答案 0 :(得分: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的默认构造函数,当我把代码放到服务器上时它没有用。
答案 1 :(得分:1)
如果要使用已定义的connectionString,则必须使用连接字符串的名称来填充&#34; sqlConnectionString&#34;属性并使用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>