Skip to content

Add Identity Components to Blazor template - #50722

Merged
mkArtakMSFT merged 18 commits into
release/8.0-rc2from
halter73/48786
Sep 22, 2023
Merged

mkArtakMSFT merged 18 commits into
release/8.0-rc2from
halter73/48786

Conversation

@halter73

@halter73 halter73 commented Sep 15, 2023 •

Copy link
Copy Markdown
Member

Add Identity Components to Blazor template

Description

This adds the option to add Identity Razor Components (*.razor files) when using the Blazor project template. This supports the same feature set as the Identity Razor Pages (*.cshtml files). We've already done an accessibility pass over these newly added components and this PR includes all the fixes for Accessibility too. As with the Identity Razor Pages, this supports local authentication (with the option to configure external login providers) and Identity management using EF Core.

Fixes #48786

Customer Impact

These Identity Razor Components have been a common request for years as noted above, because it allows Blazor developers to use Identity without needing to add Razor Pages infrastructure which would otherwise be unnecessary and doesn't integrate well with the rest of the app. For example, the Identity Razor Pages to a Blazor app would use a different layout that doesn't match the look and feel of the Razor Components that make up the rest of the app.

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

These are template only changes that only affect the brand new Blazor project template. It should have no impact on the project template output unless you opt-in to the individual auth option (other than a small fix to make the --empty option produce compileable output with all InteractivityPlatform options.)

Verification

  • Manual (required)
  • Automated

We're also adding new validation scenarios for vendors to validate periodically.

Packaging changes reviewed?

  • Yes
  • No
  • N/A

  • Finish user management components for 2fa, external login, personal data, etc...
  • Add signout link
  • Verify RegisterOnPersisting gets invoked as expected with the changes from Persist Prerendered State #50625
  • Render ShowRecoveryCodes.razor inline rather than via a redirect.
  • Add baseline tests
  • Fix BOMs
  • Validate the template in VS

@MackinnonBuck @SteveSandersonMS @javiercn @surayya-MS

@ghost ghost added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Sep 15, 2023
Comment on lines +22 to +32
<EditForm Model="Input" FormName="forgot-password" OnValidSubmit="OnValidSubmitAsync" method="post">
<DataAnnotationsValidator />
<ValidationSummary class="text-danger" role="alert" />

<div class="form-floating mb-3">
<InputText id="email" @bind-Value="Input.Email" class="form-control" autocomplete="username" aria-required="true" placeholder="[email protected]" />
<label for="email" class="form-label">Email</label>
<ValidationMessage For="() => Input.Email" class="text-danger" />
</div>
<button type="submit" class="w-100 btn btn-lg btn-primary">Reset password</button>
</EditForm>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SteveSandersonMS do we want some Enhanced magic here?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Longer term maybe. For now I'd go with whatever's been tested.

@MackinnonBuck

Copy link
Copy Markdown
Member

Note to reviewers: There are a lot of changes sitting in halter73/BlazorWebIdentity#2 that will eventually get merged into this PR.

@surayya-MS surayya-MS left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only found 2 accessibility problems

@halter73
halter73 changed the base branch from release/8.0 to release/8.0-rc2 September 20, 2023 21:45
@halter73
halter73 marked this pull request as ready for review September 20, 2023 21:46
@halter73
halter73 requested a review from a team as a code owner September 20, 2023 21:46
@halter73 halter73 added the Servicing-consider Shiproom approval is required for the issue label Sep 20, 2023
@foreach (var provider in _otherLogins)
{
<button
id="[email protected]"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must you have the id?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and everywhere else

<button class="btn btn-primary" type="submit">Download</button>
</form>
<p>
<a id="delete" href="/Account/Manage/DeletePersonalData" class="btn btn-danger">Delete</a>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<a id="delete" href="/Account/Manage/DeletePersonalData" class="btn btn-danger">Delete</a>
<a id="delete" href="Account/Manage/DeletePersonalData" class="btn btn-danger">Delete</a>

<p>
<strong>Deleting this data will permanently remove your account, and this cannot be recovered.</strong>
</p>
<form id="download-data" action="/Account/Manage/DownloadPersonalData" method="post">

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<form id="download-data" action="/Account/Manage/DownloadPersonalData" method="post">
<form id="download-data" action="Account/Manage/DownloadPersonalData" method="post">

@code {
protected override async Task OnInitializedAsync()
{
_ = await UserAccessor.GetRequiredUserAsync();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why discard this task?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's discarding the result, but still awaiting the task.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah. Strange but yes I see.

/// <summary>
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>

@SteveSandersonMS SteveSandersonMS Sep 22, 2023 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get how people are supposed to interpret this remark. How can they not use it "directly from their code"? This is literally in their code already.

Can it be phrased in a more realistic way? For example, "Do not rename or remove this parameter because it is relied on by [some external thing]"?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could probably make all these private anyway and not have to worry about customers seeing this in intellisense?

var code = await UserManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
var callbackUrl = NavigationManager.GetUriWithQueryParameters(
NavigationManager.ToAbsoluteUri("/Account/ConfirmEmail").AbsoluteUri,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
NavigationManager.ToAbsoluteUri("/Account/ConfirmEmail").AbsoluteUri,
NavigationManager.ToAbsoluteUri("Account/ConfirmEmail").AbsoluteUri,

or at least I would expect so

var user = await UserManager.FindByEmailAsync(Email);
if (user == null)
{
// Need a way to trigger a 404 from Blazor: https://github.com/dotnet/aspnetcore/issues/45654

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Need a way to trigger a 404 from Blazor: https://github.com/dotnet/aspnetcore/issues/45654

If you can set the status code on HttpContext then that would be good to do. But even if not, we shouldn't have a comment like this in the template.


<h1>Reset password confirmation</h1>
<p>
Your password has been reset. Please <a href="/Account/Login">click here to log in</a>.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Your password has been reset. Please <a href="/Account/Login">click here to log in</a>.
Your password has been reset. Please <a href="Account/Login">click here to log in</a>.

// [3] NavigateTo() does not throw an exception, meaning we're not rendering from an endpoint, so we throw
// an InvalidOperationException to indicate that we can't redirect.
navigationManager.NavigateTo(uri);
throw new InvalidOperationException($"Can only redirect when rendering from an endpoint.");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to do this? If you're trying to account for interactive rendering too, would it meet your needs to change line 33 to use NavigateTo(uri, force: true)?

};

[DoesNotReturn]
public void RedirectTo(string uri)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of code that passes in root-relative URLs (e.g., /Account/Blah). Can you confirm those will be handled correctly even if there's a nonempty pathbase? I'm not certain that's accounted for in the logic here, but may be wrong!


var redirectUrl = UriHelper.BuildRelative(
context.Request.PathBase,
$"/Account/ExternalLogin",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$"/Account/ExternalLogin",
"/Account/ExternalLogin",


namespace BlazorWeb_CSharp.Identity;

public class IdentityRevalidatingAuthenticationStateProvider : RevalidatingServerAuthenticationStateProvider

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to add a comment block above this class to indicate the basic ideas about what it does.


namespace BlazorWeb_CSharp.Identity;

public class PersistingRevalidatingAuthenticationStateProvider : RevalidatingServerAuthenticationStateProvider

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to add a comment block above this class to indicate the basic ideas about what it does.

@SteveSandersonMS

Copy link
Copy Markdown
Member

Wow, security is hard! Can we just document that users have to not be evil?

@SteveSandersonMS SteveSandersonMS left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of review comments, some of which do need to be addressed for correctness (e.g., all the root-relative URLs). In a lot of cases I stopped posting repeated comments of the same point, so if you see a general pattern of comments, please assume it applies to the entire PR and not only the specific lines I posted about.

I'm approving now on the basis that you'll make good choices about which of the feedback to address, and I won't be around later today to check.

However, could you try to get a ack from @javiercn before merging too? I know he's spent some time looking at this and may have been thinking more deeply about the actual security content. My focus has been on how understandable/idiomatic the content is.

@mkArtakMSFT

Copy link
Copy Markdown
Contributor

Merging this as is. We've identified a limitation here that we will be researching some more and will document it as a known issue in RC2 and will then address in the GA milestone.

@mkArtakMSFT
mkArtakMSFT merged commit 2fce01d into release/8.0-rc2 Sep 22, 2023
@mkArtakMSFT
mkArtakMSFT deleted the halter73/48786 branch September 22, 2023 18:50
@ghost ghost added this to the 8.0-rc2 milestone Sep 22, 2023
@iphdav

ghost commented Sep 25, 2023

Copy link
Copy Markdown

@SteveSandersonMS @MackinnonBuck
Just read your review, and because these components will be dropped into projects as source, just wanted to note Microsoft's guidance for components and the required keyword is:

  • Don't use the required modifier ... on component parameter properties. Components are usually instantiated and assigned parameter values using reflection...

I agree it looks better than having = default! everywhere. Not sure if the guidance should change, but thought it worth noting.

ghost commented Sep 25, 2023

Copy link
Copy Markdown

Hi @iphdav. It looks like you just commented on a closed PR. The team will most probably miss it. If you'd like to bring something important up to their attention, consider filing a new issue and add enough details to build context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates Servicing-approved Shiproom has approved the issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants