简体   繁体   中英

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

I am using basic authentication for my web api which has been hosted on a server. It works fine on my local with the basic authentication. But when i hosted it to the server it results in the following error

* {"Message":"An error has occurred.","ExceptionMessage":"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL *

And it works fine on the server also when i remove the basic authentication.

My connection string

<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" />    

Am using system.web.providers for the basic auth so i found this in my 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>

I did change the mode to custom and i suppose my connection string is connected to an sql server rather than sql server express?? am not sure about that!

And interestingly its showing an error at connectionStringName saying that "the attribute is not allowed"? But in my local VM it doesnt throw any error like that and thats yi guess the basic auth is working fine on my local VM.

Can anyone please tell me where i might be going wrong. Any suggestions at all would be deeply appreciated. I have been banging my head about it all day :(

Finally Resolved the issue. The problem was in my Basic Authentication filter class.

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;
    }

I have hardcoded my DataContext here but earlier i didnt pass my connection string into it. I just went for the default constructor of the WinBoutsDataContext which didnt work when i put the code on to the server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM