【发布时间】:2021-06-30 20:48:50
【问题描述】:
在授权后尝试检索当前登录用户的名称时,User.Identity.Name 为空。如果我从IdentityServer 成功登录,我似乎不明白为什么它为空,它会将我重定向回我的 blazor 项目。
在我的 blazor 项目中,我设置了以下代码:
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton(sp => new HttpClient { BaseAddress = new Uri("http://localhost:36626") }); // WebApi project
services.AddTransient<IWeatherForecastServices, WeatherForecastServices>();
services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
{
options.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.SignOutScheme = OpenIdConnectDefaults.AuthenticationScheme;
options.Authority = "https://localhost:5443"; // IdentityServer Project
options.ClientId = "interactive";
options.ClientSecret = "KEY";
options.ResponseType = "code";
options.Scope.Add("profile"); // default scope
options.Scope.Add("scope2");
options.Scope.Add("roles");
options.Scope.Add("permissions");
options.ClaimActions.MapUniqueJsonKey("role", "role");
options.SaveTokens = true;
options.GetClaimsFromUserInfoEndpoint = true;
});
services.AddScoped<TokenProvider>();
services.AddCors(options =>
{
options.AddPolicy("Open", builder => builder.AllowAnyOrigin().AllowAnyHeader());
}
);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseCors("Open");
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
Index.razor 这里变量“名称”为空:
@page "/"
@inject AuthenticationStateProvider GetAuthenticationStateAsync
<AuthorizeView>
<Authorized>
<h3>Welcome, <b>@Name</b></h3>
</Authorized>
<NotAuthorized>
<h3>You are signed out!!</h3>
</NotAuthorized>
</AuthorizeView>
@code{
private string Name;
protected override async Task OnInitializedAsync()
{
var authstate = await GetAuthenticationStateAsync.GetAuthenticationStateAsync();
var user = authstate.User;
var name = user.Identity.Name;
Name = name;
}
}
我的Login.cshtml.cs:
public class LoginModel : PageModel
{
public async Task<IActionResult> OnGetAsync(string redirectUri)
{
// just to remove compiler warning
await Task.CompletedTask;
if (string.IsNullOrWhiteSpace(redirectUri))
{
redirectUri = Url.Content("~/");
}
// If user is already logged in, we can redirect directly...
if (HttpContext.User.Identity.IsAuthenticated)
{
Response.Redirect(redirectUri);
}
return Challenge(
new AuthenticationProperties
{
RedirectUri = redirectUri
},
OpenIdConnectDefaults.AuthenticationScheme);
}
}
我的logout.cshtml.cs:
public class LogoutModel : PageModel
{
public async Task<IActionResult> OnGetAsync() // THIS METHOD DOES NOT DELETE THE AUTH COOKIES NOR REDIRECT YOU BACK TO BLAZOR APP EVEN THOUGH YOU HAVE THE REDIRECT URI OF BLAZOR
{
// just to remove compiler warning
await Task.CompletedTask;
var signOut = SignOut(
new AuthenticationProperties
{
RedirectUri = "https://localhost:5445" // Blazor Project
},
OpenIdConnectDefaults.AuthenticationScheme,
CookieAuthenticationDefaults.AuthenticationScheme);
return signOut;
}
}
前端视图:
从项目开始运行:
我点击登录,这会将我重定向到身份服务器项目:
主页视图,这里应该显示当前用户登录的位置,但它没有:
当我点击注销时,它会将我重定向到身份服务器,说我已注销:
在我的 Index.razor 文件中,是否存在导致变量 Name 为空的原因?
【问题讨论】:
-
我认为您不需要所有这些代码索引页。在
<h3>中您需要的唯一一行只是@context.User.Identity.Name
标签: c# razor blazor blazor-server-side