簡體   English   中英

對我的網站使用基本身份驗證時無法連接到服務器

[英]Unable to connect to the server when using Basic Authentication for my website

我正在對服務器上托管的Web API使用基本身份驗證。 使用基本身份驗證,在我的本地計算機上運行良好。 但是當我將其托管到服務器時,會導致以下錯誤

* {“消息”:“發生錯誤。”,“異常消息”:“與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>

我確實將模式更改為custom,並且我想我的連接字符串已連接到sql server而不是sql server express? 對此不確定!

有趣的是,它在connectionStringName上顯示了一個錯誤,指出“不允許使用屬性”? 但是,在我的本地VM中,它不會拋出任何類似的錯誤,這也就意味着基本身份驗證在我的本地VM上可以正常工作。

誰能告訴我我可能會出錯。 任何建議都將不勝感激。 我整天都在敲打我的頭:(

終於解決了這個問題。 問題出在我的基本身份驗證過濾器類中。

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的默認構造函數,當我將代碼放到服務器上時,它沒有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM