Question How to get response details after success login razor page ?

ahmedaziz

Well-known member
Joined
Feb 22, 2023
Messages
55
Programming Experience
1-3
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


json data for response shap:
{
    "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 :

How to call web api:
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 :
logic applied to json response returned:
if (response.IsSuccessStatusCode)

            {

IF(message=="success" AND status="true")

{

receive `user id and username and user role and password`

}

}

explain recieve json data.png
 
It's like any other call to any service that returns JSON

Follow my advice here: Deserialise an API response.

You go to quicktype and paste your json in there. It generates classes for you. You install Flurl and call to the login api, telling Flurl to turn the response into a class instance, then specifically in your case you'll do if(t.Message == "success" && t.Status) ...
 
Back
Top Bottom