when login success response I can't receive details of response ?

Ahmed Salah Abed Elaziz 390 Reputation points
2023-05-27T14:27:31.9166667+00:00

I work on asp.net razor page Login user name and password . I call Web API validate user

name and password . my issue I face it I can't receive data returned after login success

JSON data returned from web API after success login username and password

{
    "message": "success",
    "status": true,
    "data": {
        "userID": "9595",
        "userName": "ADC Test User",
        "userRole": "Administrator",
        "environment": "PY"
    },
    "statusCode": "0000"
}

I call api from razor page login as below :

public async Task OnPost()
{

           
            UserLoginViewModel loginview = new UserLoginViewModel();
            loginview.UserID = User.UserName;
            loginview.Password = User.vPassword;
            var json = JsonSerializer.Serialize(loginview);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://localhost:44374/api/adcxx/ValidateUser");
            request.Content = content;
            var response = await _httpClient.SendAsync(request);
            if (response.IsSuccessStatusCode)
            {
          

             
            }
}

I need to return data from login success where message=success as logic below :

 if (response.IsSuccessStatusCode)
            {
IF(message=="success" AND status="true")
{
receive `user id and username and user role and password`
}
}

image below explain what i need to do exactly

explain recieve json data

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2023-05-27T18:08:36.2433333+00:00

    If I understand correctly, you are asking how to make an HTTP request with HttpClient and deserialize the results. The official documentation covers this subject very very well.

    Make HTTP requests using IHttpClientFactory in ASP.NET Core

    HttpClient Class

    Tutorial: Create a web API with ASP.NET Core

    public async Task OnPostAsync()
    {
        UserModel model = new UserModel()
        {
            UserName = "Hello",
            Password = "password"
        };
    
        var httpClient = _httpClientFactory.CreateClient();
        using var httpResponseMessage = 
            await httpClient.PostAsJsonAsync("https://localhost:7190/api/Login", model);
    
        httpResponseMessage.EnsureSuccessStatusCode();
    
        AuthenticationResponse response = await httpResponseMessage.Content.ReadFromJsonAsync<AuthenticationResponse>();
    }
    
        public class AuthenticationResponse
        {
            public string message { get; set; }
            public bool status { get; set; }
            public Data data { get; set; }
            public string statusCode { get; set; }
        }
    
        public class Data
        {
            public string userID { get; set; }
            public string userName { get; set; }
            public string userRole { get; set; }
            public string environment { get; set; }
        }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.