<AuthorizeView> <Authorized> <a href="Identity/Account/Manage">Hello, @context.User.Identity.Name!</a> <form method="post" action="Identity/Account/LogOut"> <button type="submit" class="nav-link btn btn-link">Log out</button> </form> </Authorized> <NotAuthorized> <a href="Identity/Account/Register">Register</a> <a href="Identity/Account/Login">Log in</a> </NotAuthorized> </AuthorizeView>
<CascadingAuthenticationState> <Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true"> <Found Context="routeData"> <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" /> </Found> <NotFound> <LayoutView Layout="@typeof(MainLayout)"> <p>Sorry, there's nothing at this address.</p> </LayoutView> </NotFound> </Router> </CascadingAuthenticationState>
app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication();//add app.UseAuthorization();//add app.UseEndpoints(endpoints => { endpoints.MapBlazorHub(); endpoints.MapFallbackToPage("/_Host"); });
public class BlazorTest228User : IdentityUser { [PersonalData] public string CustomName { get; set; } }
public class InputModel { [Required] [DataType(DataType.Text)] [Display(Name = "Custom name")] public string CustomName { get; set; } [Phone] [Display(Name = "Phone number")] public string PhoneNumber { get; set; } } private async Task LoadAsync(BlazorTest228User user) { var userName = await _userManager.GetUserNameAsync(user); var phoneNumber = await _userManager.GetPhoneNumberAsync(user); Username = userName; Input = new InputModel { CustomName = user.CustomName, PhoneNumber = phoneNumber }; } if (Input.CustomName != user.CustomName) { user.CustomName = Input.CustomName; } await _userManager.UpdateAsync(user); await _signInManager.RefreshSignInAsync(user);
<form id="profile-form" method="post"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="Input.CustomName"></label> <input asp-for="Input.CustomName" class="form-control" /> </div> <div class="form-group"> <label asp-for="Username"></label> <input asp-for="Username" class="form-control" disabled /> </div>
public class InputModel { [Required] [DataType(DataType.Text)] [Display(Name = "Custom name")] public string CustomName { get; set; } [Required] [EmailAddress] [Display(Name = "Email")] public string Email { get; set; } public async Task<IActionResult> OnPostAsync(string returnUrl = null) { returnUrl ??= Url.Content("~/"); ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); if (ModelState.IsValid) { var user = new BlazorTest228User { CustomName = Input.CustomName, UserName = Input.Email, Email = Input.Email }; var result = await _userManager.CreateAsync(user, Input.Password);
<div asp-validation-summary="All" class="text-danger"></div> <div class="form-group"> <label asp-for="Input.CustomName"></label> <input asp-for="Input.CustomName" class="form-control" /> <span asp-validation-for="Input.CustomName" class="text-danger"></span> </div> <div class="form-group"> <label asp-for="Input.Email"></label> <input asp-for="Input.Email" class="form-control" /> <span asp-validation-for="Input.Email" class="text-danger"></span> </div>
@using Microsoft.AspNetCore.Identity @using BlazorTest228.Areas.Identity.Data @inject UserManager<BlazorTest228User> UserManager <AuthorizeView> <Authorized> @*<a href="Identity/Account/Manage">Hello, @context.User.Identity.Name!</a>*@ <a href="Identity/Account/Manage"> @UserManager.GetUserAsync(context.User).Result.CustomName </a> <form method="post" action="Identity/Account/LogOut"> <button type="submit" class="nav-link btn btn-link">Log out</button> </form> </Authorized> <NotAuthorized> <a href="Identity/Account/Register">Register</a> <a href="Identity/Account/Login">Log in</a> </NotAuthorized> </AuthorizeView>
public class IdentityHostingStartup : IHostingStartup { public void Configure(IWebHostBuilder builder) { builder.ConfigureServices((context, services) => { services.AddDbContext<BlazorTest228Context>(options => options.UseSqlServer( context.Configuration.GetConnectionString("BlazorTest228ContextConnection"))); services.AddDefaultIdentity<BlazorTest228User>(options => options.SignIn.RequireConfirmedAccount = false) .AddRoles<IdentityRole>() .AddEntityFrameworkStores<BlazorTest228Context>(); }); } }
<CascadingAuthenticationState> <Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true"> <Found Context="routeData"> <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"> <NotAuthorized> <h1>Sorry</h1> <p>You're not authorized to reach this page.</p> <p>You may need to log in as a different user.</p> </NotAuthorized> <Authorizing> <h1>Authorization in progress</h1> <p>Only visible while authorization is in progress.</p> </Authorizing> </AuthorizeRouteView> </Found> <NotFound> <LayoutView Layout="@typeof(MainLayout)"> <h1>Sorry</h1> <p>Sorry, there's nothing at this address.</p> </LayoutView> </NotFound> </Router> </CascadingAuthenticationState>
@page "/counter" @attribute [Authorize] <h1>Counter</h1>
<AuthorizeView> <Authorized> <h1>Hello, @context.User.Identity.Name!</h1> <p>登陆后显示的内容</p> </Authorized> <NotAuthorized> <h1>未登陆时的信息</h1> <p>用户尚未登陆</p> </NotAuthorized> </AuthorizeView>