// -------------------------- Add to .csprpoj file
net5.0
aspnet-{Your Project Name}-{Generate A Guid}
// -------------------------- App.Razor
Sorry, there's nothing at this address.
// -------------------------- Shared\MainLayout.razor
@inherits LayoutComponentBase
// -------------------------- Shared\LoginDisplay.razor
@using Microsoft.Identity.Web
@using Microsoft.Extensions.Options
@inject IOptionsMonitor microsoftIdentityOptions
@if (canEditProfile)
{
Hello, @context.User.Identity.Name!
}
else
{
Hello, @context.User.Identity.Name!
}
Log out
Log in
@code {
private bool canEditProfile;
protected override void OnInitialized()
{
var options = microsoftIdentityOptions.CurrentValue;
canEditProfile = !string.IsNullOrEmpty(options.EditProfilePolicyId);
}
}
// -------------------------- Add to Startup.cs:
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAdB2C"));
services.AddControllersWithViews()
.AddMicrosoftIdentityUI();
services.AddAuthorization(options =>
{
// By default, all incoming requests will be authorized according to the default policy
options.FallbackPolicy = options.DefaultPolicy;
});
services.AddServerSideBlazor()
.AddMicrosoftIdentityConsentHandler();
// after app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
endpoints.MapControllers();
// -------------------------- Add to appsettings.json:
"AzureAdB2C": {
"Instance": "your-instance-url",
"ClientId": "your-ad-b2c-application-id",
"CallbackPath": "/signin-oidc",
"Domain": "your-domain",
"SignedOutCallbackPath": "/signout",
"SignUpSignInPolicyId": "your-signin-sightout-policy-id",
"ResetPasswordPolicyId": "your-password-reset-policy-id",
"EditProfilePolicyId": "",
"ClientSecret": "your-client-secret"
},