Answered by:
Unable to access ApplicationUser on Production

Question
-
User-2611850 posted
Hello Everyone,
I have an odd error on my Blazor Webassembly .NET 5 page.
On the Production the Username is always NULL. The Controller is able to read it but serialization seems not to work maybe?
I would appreciate any help! Thanks a lot, kind blessings, Andreas
Blazor Server UserController:
[HttpGet] public async Task<ApplicationUser> GetUserAsync() { try { return await _context.Users.FirstAsync(); } catch (Exception ex) { _logger.LogError(ex, "Error in UserController User Get"); } return null; }
Blazor Client User.razor:
protected override async Task OnInitializedAsync() { try { var result = await Http.GetFromJsonAsync<ApplicationUser>("User");
//result.Username is always null here on Production Environment but not on Development. StateHasChanged(); } catch (Exception ex) { Serilog.Log.Error(ex, "ReneeMcGuire.Client.Users.razor: Exception in OnInitializedAsync"); } }Monday, February 1, 2021 8:57 PM
Answers
-
User-2611850 posted
Was able to resolve the Problem by using A simplified Model class of User and passed it on instead of ApplicationUser.
Now it works, kind blessings
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 3, 2021 3:55 PM
All replies
-
User-474980206 posted
As the controller as coded returns null on thrown errors, rather than returning an error (poor design), it’s probably error Ing out. That does you log say?
Tuesday, February 2, 2021 3:52 PM -
User-2611850 posted
Thanks a lot for your response!
Production: result contains the return value: {"UserName":null}
Development: result contains the return value: {"Id":"xxxxx-xxx-xxxxx-xxxx-xxxxx","UserName":"xx@xxxxx.com","NormalizedUserName":"xx@xx.COM","Email":"xx@xxxx.com","NormalizedEmail":"xx@xxx.COM","EmailConfirmed":true,"PasswordHash":"xxxxxxx","SecurityStamp":"xxxxxx","ConcurrencyStamp":"xxxxxx","PhoneNumber":null,"PhoneNumberConfirmed":false,"TwoFactorEnabled":false,"LockoutEnd":null,"LockoutEnabled":true,"AccessFailedCount":0}
Both Production & Development use the same code (simplified) above. one returns the full ApplicationUser production on the other hand only return {"UserName":null}. I have no idea why this is?
Thanks would appreciate any help! Thanks, kind blessings, AndreasTuesday, February 2, 2021 5:35 PM -
User-2611850 posted
Hello Everyone,
After further investigation on the Production code I replaced
await Http.GetFromJsonAsync<ApplicationUser>("User")
with:
await Http.GetStringAsync("User")
The Server Api returns this Json (serialized it to Json):
"{"Id":"xxxx-xxx","UserName":"xxx@xxx.com","NormalizedUserName":xxx@xxx.COM","Email":"xxx@xxx.com","NormalizedEmail":"xxx@xxx.COM","EmailConfirmed":true,"PasswordHash":"xxx","SecurityStamp":"xxx","PhoneNumber":null,"PhoneNumberConfirmed":false,"TwoFactorEnabled":false,"LockoutEnd":null,"LockoutEnabled":true,"AccessFailedCount":0}"
The Client now using GetStringAsync instead of GetFromJsonAsync (returned {"UserName":null}):
{"id":"xxxx-xxx","userName":"xxx@xxx.com","normalizedUserName":"xxx@xxx.COM","email":"xxx@xxx.com","normalizedEmail":"xxx@xxx.COM","emailConfirmed":true,"passwordHash":"xxx","securityStamp":"xxx","concurrencyStamp":"xxx","phoneNumber":null,"phoneNumberConfirmed":false,"twoFactorEnabled":false,"lockoutEnd":null,"lockoutEnabled":true,"accessFailedCount":0}
I Noticed that the Json Keys arriving on the client side are all NOT capitalized but on the Server side they are. Does in GetFromJsonAsync the Json serializazion somehow break because of the capitalization? Bot Json objects on client and server are identical otherwise and parse error free in a json editor. I have no idea what the problem may be.
I would apprecaite any help so much! Thanks a lot, kind blessings, Andreas
Wednesday, February 3, 2021 1:01 PM -
User-2611850 posted
Dear Bruce,
The Log has no errors, because I isolated the code down to the problem which is {"UserName":null} is returned instead of the full Entity which then lets the Griedview crash. It seems the problem on the Production is somehow related that in the Json, keys are NOT capitalized therefore it is not converted and returns NULL.
Thanks, Kind blessings, Andreas
Wednesday, February 3, 2021 1:26 PM -
User-2611850 posted
Was able to resolve the Problem by using A simplified Model class of User and passed it on instead of ApplicationUser.
Now it works, kind blessings
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 3, 2021 3:55 PM