ASP.NET WEB API - calling WEB API from a site that has login page (FormsAuthentication) RRS feed

  • Question

  • User135423268 posted

    Good Day Everyone

    I'm trying to call the WEB API from another site which has authentication/Login page that is using FormsAuthentication, I'm successfully calling the API when I'm typing the url below"


    but when I'm calling either in Ajax/JSON or in the MVC Controller, It always return the WHOLE LOGIN PAGE HTML CODES, below is my code on the controller.

        Public Async Function GetEmployees() As Task(Of List(Of tblEmployeesList))
                Dim getTblEmployeesList As New List(Of tblEmployeesList)
                Using client As New HttpClient
                    client.BaseAddress = New Uri(BaseUrl)
                    client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
                    Dim res As HttpResponseMessage = Await client.GetAsync("api/mywa/getemployeelist")
                    If res.IsSuccessStatusCode = True Then
                        Dim EmployeesResponse = res.Content.ReadAsStringAsync().Result
                        getTblEmployeesList = JsonConvert.DeserializeObject(Of List(Of tblEmployeesList))(EmployeesResponse)
                    End If
                    Return getTblEmployeesList
                End Using
            Catch ex As Exception
            End Try
        End Function

    when i try to test it, an error occur and inside the res.Content.ReadAsStringAsync().Result is the whole html script of the login page

    I don't know why this is occur but if there is someone who encounter this and fix this, kindly teach me masters.

    Thanks and Regards

    Thursday, August 23, 2018 1:51 AM

All replies

  • User1724605321 posted

    HI amendoza,

    It seems the web app and web api both have authentication , so may i know what's the scenario you are facing ? How would you implement SSO between the two websites , use identity provide like identity server  to issue access token to access web api ? Please explain more about that .

    Best Regards,

    Nan Yu

    Friday, August 24, 2018 5:16 AM