How can I use OIDC authentication in server-side Blazor?
I used this method, but somehow it's not right because @attribute [AllowAnonymous] doesn't really work. So I used the [Authorized] attribute instead of [AllowAnonymous] and then removed RequireAuthenticatedUser, but OIDC does not redirect the client to the server login page.
I checked Steve Sanderson's GitHub article about authentication and authorization in Blazor, but he doesn't talk about OIDC.
Here is my Startup class:
services.AddAuthentication(config =>{ config.DefaultScheme = "Cookie"; config.DefaultChallengeScheme = "oidc";}) .AddCookie("Cookie") .AddOpenIdConnect("oidc", config => { config.Authority = "https://localhost:44313/"; config.ClientId = "client"; config.ClientSecret = "secret"; config.SaveTokens = true; config.ResponseType = "code"; config.SignedOutCallbackPath = "/"; config.Scope.Add("openid"); config.Scope.Add("api1"); config.Scope.Add("offline_access"); });services.AddMvcCore(options =>{ var policy = new AuthorizationPolicyBuilder() .RequireAuthenticatedUser() // site-wide auth .Build(); options.Filters.Add(new AuthorizeFilter(policy));});