C
C#2y ago
Nickolaki

❔ Identity Framework Users with OAuth clients.

https://i.imgur.com/A5jjfhk.png I'm following the following drawing in my NextJs frontend and .Net 6 Web API. My implementation is working as the diagram states, however, I've run into the following issue: - The JWT that I sign and give back to my frontend has the Id and Username from the Github but I'm using Identity Framework to store my users. Technically my app doesn't need an identity user to be stored to run, my object UserId property would just have a Github, Google, Facebook user ids. But as you can tell this is far from ideal, what do you think would be a good solution here? I had thought of maybe checking if a user with that email exists in the database first, if not create the user. But I believe Identity Framework would require the users password which I cannot provide. Here is some code snippets to give further context:
[HttpPost("exchange-token/github", Name = "ExchangeGithubToken")]
public async Task<ActionResult<ExchangeTokenResponse>> ExchangeGithubToken([FromBody] ExchangeTokenRequest request)
{
var result = await _authenticationService.ExchangeGithubToken(request);

return Ok(result);
}
[HttpPost("exchange-token/github", Name = "ExchangeGithubToken")]
public async Task<ActionResult<ExchangeTokenResponse>> ExchangeGithubToken([FromBody] ExchangeTokenRequest request)
{
var result = await _authenticationService.ExchangeGithubToken(request);

return Ok(result);
}
public async Task<ExchangeTokenResponse> ExchangeGithubToken(ExchangeTokenRequest request)
{
var httpClient = _httpClientFactory.CreateClient("Github");

httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", request.Token);

var response = await httpClient.GetAsync("/user");

if (!response.IsSuccessStatusCode) throw new NotImplementedException();

var content = await response.Content.ReadAsStringAsync();

var user = JsonSerializer.Deserialize<GithubUser>(content);

var token = _jwtTokenGenerator.GenerateToken(user.Id.ToString(), user.Login);

return new ExchangeTokenResponse(token);
}
public async Task<ExchangeTokenResponse> ExchangeGithubToken(ExchangeTokenRequest request)
{
var httpClient = _httpClientFactory.CreateClient("Github");

httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", request.Token);

var response = await httpClient.GetAsync("/user");

if (!response.IsSuccessStatusCode) throw new NotImplementedException();

var content = await response.Content.ReadAsStringAsync();

var user = JsonSerializer.Deserialize<GithubUser>(content);

var token = _jwtTokenGenerator.GenerateToken(user.Id.ToString(), user.Login);

return new ExchangeTokenResponse(token);
}
Any help/advice would be much appreciated! 🤘
Imgur
1 Reply
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server