File: AuthController.cs

package info (click to toggle)
pysignalr 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 468 kB
  • sloc: python: 1,192; cs: 121; xml: 37; makefile: 28
file content (65 lines) | stat: -rw-r--r-- 2,220 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;

namespace AspNetAuthExample.Controllers;

// Define the route for the API controller
[Route("api/[controller]")]
[ApiController]
public class AuthController : ControllerBase
{
    // Define the POST endpoint for login
    [HttpPost("login")]
    public IActionResult Login([FromBody] LoginModel login)
    {
        // Check if the provided username and password match the predefined values
        if (login.Username == "test" && login.Password == "password")
        {
            // Generate a JWT token if credentials are correct
            var token = GenerateToken();
            // Return the token in the response
            return Ok(new { token });
        }
        // Return Unauthorized status if credentials are incorrect
        return Unauthorized();
    }

    // Method to generate a JWT token
    private string GenerateToken()
    {
        // Define the security key using a secret key
        var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("yoursecretkeyheretoSignalRserver"));
        // Define the signing credentials using HMAC-SHA256 algorithm
        var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);

        // Define the claims to be included in the token
        var claims = new[]
        {
            new Claim(JwtRegisteredClaimNames.Sub, "testuser"),
            new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
        };

        // Create the JWT token with specified claims and expiration time
        var token = new JwtSecurityToken(
            issuer: null,
            audience: null,
            claims: claims,
            expires: DateTime.Now.AddMinutes(30),
            signingCredentials: credentials);

        // Return the serialized token as a string
        return new JwtSecurityTokenHandler().WriteToken(token);
    }
}

// Model to represent the login request payload
public class LoginModel
{
    // Username property
    public string Username { get; set; }
    // Password property
    public string Password { get; set; }
}