如何在我的.net应用程序中避免这个错误

yizd12fk  于 2023-08-08  发布在  .NET
关注(0)|答案(1)|浏览(110)

这是我的错
SocketException:无法建立连接,因为目标计算机主动拒绝连接。
我用这个密码

public class AppService : IAppService
{
    public async Task<MainResponse> AuthenticateUser(LoginModel loginModel)
    {
        var returnResponse = new MainResponse();

        using (var client = new HttpClient())
        {
            var url = $"{Setting.BaseUrl}{UserAPIs.AuthenticateUser}";

            var serializedStr = JsonConvert.SerializeObject(loginModel);

            var response = await client.PostAsync(url, new StringContent(serializedStr, Encoding.UTF8, "application/json"));

            if (response.IsSuccessStatusCode)
            {
                string contentStr = await response.Content.ReadAsStringAsync();
                returnResponse = JsonConvert.DeserializeObject<MainResponse>(contentStr);
            }
        }

        return returnResponse;
    }
}

字符串
这是我的控制器代码的API所有我的端口号是正确的和所有其他API的运行正确我只是不能得到通过登录的用户名和密码

[AllowAnonymous]
    [HttpPost("AuthenticateUser")]
    public async Task<IActionResult> AuthenticateUser(LoginModel authenticateUser)
    {
        try
        {
            var user = await _userManager.FindByNameAsync(authenticateUser.UserName);
            if (user == null) return Unauthorized();

            bool isValidUser = await _userManager.CheckPasswordAsync(user, authenticateUser.Password);

            if (isValidUser)
            {
                string accessToken = GenerateAccessToken(user);
                var refreshToken = GenerateRefreshToken();
                await _userManager.UpdateAsync(user);

                var role = await _userManager.GetRolesAsync(user);

                int? roleID = null;
                switch (role?.FirstOrDefault())
                {
                    case "Admin":
                        roleID = (int)RoleEnum.Admin;
                        break;
                    case "Student":
                        roleID = (int)RoleEnum.Student;
                        break;
                    case "Teacher":
                        roleID = (int)RoleEnum.Teacher;
                        break;
                }

                var response = new MainResponse
                {
                    Content = new AuthenticationResponse
                    {
                        RefreshToken = refreshToken,
                        AccessToken = accessToken,
                        RoleID = roleID
                    },
                    IsSuccess = true,
                    ErrorMessage = ""
                };
                return Ok(response);
            }

            else
            {
                return Unauthorized();
            }
        }
        catch (Exception ex)
        {
            var response = new MainResponse
            {
                Content = new AuthenticationResponse
                {
                    RefreshToken = null,
                    AccessToken = null,
                    RoleID = null,
                    DeviceToken = null
                },
                IsSuccess = true,
                ErrorMessage = ex.Message
            };
            return Ok(response);
        }
    }

c0vxltue

c0vxltue1#

试试这些。
1.确保API处于Up状态。
1.查看网址
1.在代码中添加异常处理
public async Task AuthenticateUser(LoginModel){ var returnResponse = new MainResponse();

try
{
    using (var client = new HttpClient())
    {
        var url = $"{Setting.BaseUrl}{UserAPIs.AuthenticateUser}";

        var serializedStr = JsonConvert.SerializeObject(loginModel);

        var response = await client.PostAsync(url, new StringContent(serializedStr, Encoding.UTF8, "application/json"));

        if (response.IsSuccessStatusCode)
        {
            string contentStr = await response.Content.ReadAsStringAsync();
            returnResponse = JsonConvert.DeserializeObject<MainResponse>(contentStr);
        }
    }
}
catch (Exception ex)
{
    // Log or display the exception details for troubleshooting.
    Console.WriteLine($"Error: {ex.Message}");
    // You can also throw the exception if needed.
    // throw;
}

return returnResponse;

字符串
}

相关问题