我正在为我的web api使用基本身份验证,该api已托管在服务器上。它通过基本身份验证在我的本地工作正常。但是当我将它托管到服务器时,会导致以下错误
* {“消息”:“发生错误。”,“ExceptionMessage”:“建立与SQL Server的连接时发生与网络相关或特定于实例的错误。服务器未找到或未找到验证实例名称是否正确并且SQL Server配置为允许远程连接。(提供程序:命名管道提供程序,错误:40 - 无法打开与SQL *的连接
当我删除基本身份验证时,它在服务器上也能正常工作。
我的连接字符串
<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" />
我使用system.web.providers进行基本身份验证,所以我在web.config中找到了这个
<!--
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>
我确实将模式更改为自定义,我想我的连接字符串是连接到sql server而不是sql server express?我不确定!
有趣的是,它在connectionStringName上显示错误,说“该属性不被允许”?但是在我的本地VM中它不会抛出任何类似的错误,这就是我认为基本的auth在我的本地VM上工作正常。
任何人都可以告诉我哪里可能出错了。任何建议都将深表感谢。我一整天都在喋喋不休地说:(
答案 0 :(得分:0)
最后解决了这个问题。问题出在我的基本身份验证过滤器类中。
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的默认构造函数,当我把代码放到服务器上时它没有用。