C# 为什么@attribute[Authorize]不起作用?
我有一个blazor服务器端项目。我的目标是在与真实api通信之前创建模拟Cookie身份验证。如果用户未经身份验证,我想将用户重定向到登录页面 我的代码点: AuthenticateController.cs:C# 为什么@attribute[Authorize]不起作用?,c#,authentication,blazor,blazor-server-side,authorize-attribute,C#,Authentication,Blazor,Blazor Server Side,Authorize Attribute,我有一个blazor服务器端项目。我的目标是在与真实api通信之前创建模拟Cookie身份验证。如果用户未经身份验证,我想将用户重定向到登录页面 我的代码点: AuthenticateController.cs: [Route("/[controller]")] [ApiController] public class AuthenticateController : ControllerBase { [HttpPost]
[Route("/[controller]")]
[ApiController]
public class AuthenticateController : ControllerBase
{
[HttpPost]
public async Task<ActionResult> Login(UserLoginDto userLoginDto)
{
// Clear the existing external cookie
await HttpContext
.SignOutAsync(
CookieAuthenticationDefaults.AuthenticationScheme);
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Name, userLoginDto.UserName));
identity.AddClaim(new Claim("Token", "Ilyas12345"));
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(identity),
new AuthenticationProperties
{
IsPersistent = true,
AllowRefresh = true,
ExpiresUtc = DateTime.UtcNow.AddDays(1)
}).ConfigureAwait(false);
return Redirect("/");
}
}
My startup.cs配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
services.AddHttpClient();
services.AddHttpClient("api", hc =>
{
hc.BaseAddress = new Uri("http://localhost:5000/");
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
登录事件后:
用户似乎已通过身份验证,但程序始终重定向到登录页面。
你知道为什么吗?你如何调用其他端点?请求中是否包含cookie?我还没有编写其他端点,但我会像上面那样编写。是的,它包括在内,但属性不起作用。为什么不使用它呢?
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
services.AddHttpClient();
services.AddHttpClient("api", hc =>
{
hc.BaseAddress = new Uri("http://localhost:5000/");
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<RedirectToLogin />
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
@inject NavigationManager Navigation
@code {
protected override async Task OnInitializedAsync()
{
Navigation.NavigateTo("login", true);
}
}