jhammond
jhammond

Reputation: 2086

Handling quotes in Blazor

I've got a line like this in my Blazor Web Assembly app:

<Calendar User="@context.User.Claims.Where(user => user.Type == "sid")"></Calendar>

But VS doesn't like the fact that "sid" is in quotes because it messes up the User="" attribute. How do I handle this?

Also, side question, is this the proper way to get a user Id in blazor web assembly? This seems like the easiest way, but I'm new to authentication so I don't know if I'm violating a best practice by doing this.

Thanks!

Upvotes: 9

Views: 4495

Answers (2)

Ryan Wilson
Ryan Wilson

Reputation: 10765

Use parenthesis between the @ and the C# code:

<Calendar User="@(context.User.Claims.Where(user => user.Type == "sid"))"></Calendar>

As to your second question, assuming you are using Asp.Net for your back end code, you can get the User object from the HttpContext.Current.User of the user request server side, you can read more about this here (https://learn.microsoft.com/en-us/dotnet/api/system.security.claims.claimsprincipal?view=netcore-3.1)

Upvotes: 17

Douglas Riddle
Douglas Riddle

Reputation: 883

You can use parenthesis @( ) to make it a code block

<Calendar User="@(context.User.Claims.Where(user => user.Type == "sid"))" />

Upvotes: 6

Related Questions