user1688401
user1688401

Reputation: 1869

how to send and get username and password parameter from postman to code with basic auth asp.net

I am trying to make api

here is my login for get autherization token

  public IActionResult Login(string Username,string Password)
    {
        if (ModelState.IsValid)
        {
            string user = GetAuth(Username, Password);
            if (user == null)
            {
                return Unauthorized();
            }

            var claims = new[]
            {
        new System.Security.Claims.Claim(JwtRegisteredClaimNames.Sub, Username),
        new System.Security.Claims.Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
    };

            var token = new System.IdentityModel.Tokens.Jwt.JwtSecurityToken
            (
                issuer: "" ,
                audience: "",//appsettings.json içerisinde bulunan audince değeri
                claims: claims,
                expires: DateTime.UtcNow.AddDays(30), // 30 gün geçerli olacak
                notBefore: DateTime.UtcNow,
                signingCredentials: new Microsoft.IdentityModel.Tokens.SigningCredentials(new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(System.Text.Encoding.UTF8.GetBytes("aaa wssadsa adsad sa  rasd as")),//appsettings.json içerisinde bulunan signingkey değeri
                        Microsoft.IdentityModel.Tokens.SecurityAlgorithms.HmacSha256)
            ) ;
            Microsoft.IdentityModel.Logging.IdentityModelEventSource.ShowPII = true;

            return Ok(new { token = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler().WriteToken(token) });
        }
        else
        {
            return BadRequest();
        }

    }

Now I want to test this using postman.In postman UI in Authorization tab I entered username and password and I click send buton,it call my function but Username and password is null

what is my mistake?

enter image description here

Upvotes: 0

Views: 3734

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239430

You're mixing and matching concepts here. Basic auth doesn't just mean "uses a username and password"; it's a particular authentication scheme that is the authorization token. For example, just as you might authorize an API call with an auth token via sending a header like Authorization: Bearer {token}, you can authorize via Authorization: Basic {base64-encode user:pass}.

What you're doing here is a login. In other words, you might do this first to return the auth token that would then later be passed via Authorization: Bearer {token}. Here, the username and password values need to come from the request body, not an Authorization header.

Upvotes: 1

Related Questions