Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

authenticate changes #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Empty file.
5 changes: 3 additions & 2 deletions Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public IActionResult Authenticate(AuthenticateRequest model)
[HttpGet]
public IActionResult GetAll()
{
var users = _userService.GetAll();
return Ok(users);
//var users = _userService.GetAll();
var v = HttpContext.Items["Token"];
return Ok(v);
}
}
9 changes: 8 additions & 1 deletion Helpers/JwtMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ private void attachUserToContext(HttpContext context, IUserService userService,

var jwtToken = (JwtSecurityToken)validatedToken;
var userId = int.Parse(jwtToken.Claims.First(x => x.Type == "id").Value);

var userName = jwtToken.Claims.First(x => x.Type == "username").Value;
var tokenExpireat=jwtToken.ValidTo;
System.Collections.Hashtable ht=new System.Collections.Hashtable();
ht.Add("userId", userId);
ht.Add("userName", userName);
ht.Add("tokenExpireat", tokenExpireat);
context.Items["Token"] = ht;
// attach user to context on successful jwt validation
context.Items["User"] = userService.GetById(userId);

}
catch
{
Expand Down
18 changes: 9 additions & 9 deletions Models/AuthenticateResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ namespace WebApi.Models;

public class AuthenticateResponse
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Username { get; set; }
//public int Id { get; set; }
//public string FirstName { get; set; }
//public string LastName { get; set; }
//public string Username { get; set; }
public string Token { get; set; }


public AuthenticateResponse(User user, string token)
public AuthenticateResponse(/*User user,*/ string token)
{
Id = user.Id;
FirstName = user.FirstName;
LastName = user.LastName;
Username = user.Username;
//Id = user.Id;
//FirstName = user.FirstName;
//LastName = user.LastName;
//Username = user.Username;
Token = token;
}
}
2 changes: 2 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

// configure DI for application services
services.AddScoped<IUserService, UserService>();

}

var app = builder.Build();

// configure HTTP request pipeline
{
// global cors policy

app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
Expand Down
6 changes: 3 additions & 3 deletions Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public AuthenticateResponse Authenticate(AuthenticateRequest model)
// authentication successful so generate jwt token
var token = generateJwtToken(user);

return new AuthenticateResponse(user, token);
return new AuthenticateResponse(token);
}

public IEnumerable<User> GetAll()
Expand All @@ -63,8 +63,8 @@ private string generateJwtToken(User user)
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[] { new Claim("id", user.Id.ToString()) }),
Expires = DateTime.UtcNow.AddDays(7),
Subject = new ClaimsIdentity(new[] { new Claim("id", user.Id.ToString()), new Claim("username", user.Username.ToString()) }),
Expires = DateTime.UtcNow.AddHours(1),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
Expand Down