BakaPresident
BakaPresident
Explore posts from servers
CC#
Created by BakaPresident on 6/19/2023 in #help
❔ Getting Identity from [Authorize]
Hey there, since i'm using [Authorize] i thought i should be able to get UserID from var userId = HttpContext.User.FindFirstValue("nameid"); But userId keeps returning null. For context, this is how i build the claims in my AuthService
var claims = new List<Claim>
{
new Claim(JwtRegisteredClaimNames.Sub, user.Email),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Email, user.Email),
new Claim(JwtRegisteredClaimNames.NameId, user.Id)
}.Union(roleClaims).Union(userClaims);
var claims = new List<Claim>
{
new Claim(JwtRegisteredClaimNames.Sub, user.Email),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Email, user.Email),
new Claim(JwtRegisteredClaimNames.NameId, user.Id)
}.Union(roleClaims).Union(userClaims);
3 replies
CC#
Created by BakaPresident on 6/19/2023 in #help
❔ Entity Framework not required foreign key as required?
I'm currently using ASP.Net Identity as my user scaffold. My user currently looks like
public class User : IdentityUser
{
// Add additional data here
public string DisplayName { get; set; }

public ICollection<Bookshelf> Bookshelfs { get; } = new List<Bookshelf>();
}
public class User : IdentityUser
{
// Add additional data here
public string DisplayName { get; set; }

public ICollection<Bookshelf> Bookshelfs { get; } = new List<Bookshelf>();
}
I've added UserGenre and Genre like this.
public class Genre
{
public int Id { get; set; }
[Required]
public string Name { get; set; }

public List<UserGenre> UserGenres { get; } = new();
}
public class Genre
{
public int Id { get; set; }
[Required]
public string Name { get; set; }

public List<UserGenre> UserGenres { get; } = new();
}
public class UserGenre
{
public int Id { get; set; }

public string UserId { get; set; }
public User User { get; set; } = null!;

public int GenreId { get; set; }
public Genre Genre { get; set; } = null!;
}
public class UserGenre
{
public int Id { get; set; }

public string UserId { get; set; }
public User User { get; set; } = null!;

public int GenreId { get; set; }
public Genre Genre { get; set; } = null!;
}
6 replies