【问题标题】:Api Call returning an exceptionApi 调用返回异常
【发布时间】:2020-12-12 00:47:11
【问题描述】:

我正在使用 Monogame 开发游戏,我想访问我的 Api 以便我可以登录,但它总是返回异常。如果我使用端口 80,我会得到以下一个 No connection could be made because the target machine actively refused it,如果我使用端口 5000,我会得到一个 401: Not authorized 。 通过在控制台中打印,我可以得出结论,我的尝试在 response = await client.GetStringAsync(builder.Uri.AbsoluteUri); 行被中断 我的代码有问题吗?

通讯类

public class Communication
    {
        private readonly HttpClient client = new HttpClient();
        private const string Uri = "http://localhost:5000/";
        private const int Port = 5000;

        public Communication()
        {
           
        }
        public async Task<User> Login(string username, string password)
        {
            string response;
            User user = null;

            try
            {
                var builder = new UriBuilder(Uri + "/Api/Account/Login/")
                {
                    Port = Port
                };
                builder.Query = $"Username={username}&Password={password}";
                Debug.WriteLine("Chegou Aqui!!!!")
                response = await client.GetStringAsync(builder.Uri.AbsoluteUri);

                if (response == "OK")
                {

                    user = JsonConvert.DeserializeObject<User>(response);

                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("\tERROR {0}", ex.Message);

            }

            return user;
        }



    }

我的Api登录方式

 [AllowAnonymous]
        [HttpPost("Login")]
        public IActionResult Authenticate([FromBody]AuthenticateModel userModel)
        {
            
            var user = _userService.Authenticate(userModel.Username, userModel.Password);

            if(user == null)
            {
                return BadRequest(new { message = "Username or Password invalid" });
            }

            var tokenHandler = new JwtSecurityTokenHandler();
            
            var key = Encoding.ASCII.GetBytes(_appSettings.Secret);

            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, user.Id.ToString())
                }),
                Expires = DateTime.UtcNow.AddDays(7),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };

            var token = tokenHandler.CreateToken(tokenDescriptor);

            var tokenString = tokenHandler.WriteToken(token);

            return Ok(new
            {
                Id = user.Id,
                UserName = user.Username,
                Token = tokenString
            }) ;
        }

【问题讨论】:

    标签: c# .net rest .net-core monogame


    【解决方案1】:

    This article或许能帮到你

    您可以检查您的防火墙设置,或者如果您的系统中安装了任何防病毒软件,请务必检查它的设置。

    您收到错误 401: Not authorized,因为 HTTP 请求默认配置在端口 80 上,而不是任何其他端口。

    如果问题仍然存在,请检查您的路由器或交换机配置,如果有任何在您的服务器路由中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-05
      • 2018-10-09
      • 1970-01-01
      相关资源
      最近更新 更多