question

Patrick-4313 avatar image
0 Votes"
Patrick-4313 asked miwan2-msft answered

Blazor Server - Custom Authentication

Hello together,

an old winforms application should be re-designed / migrated as web application, here comes Blazor Server in. In the winforms application each user have to authenticate at startup against an oracle database, the application tries to connect with the individiual oracle user accounts to the database and with established connection, the user was "authenticated" the hole time, until the application was closed.

For the new Blazor Server application I need the same custom authentication process, the web application is used only within Intranet.

Pre-Work
========

The following describes my already created steps:

In the custom ServerAuthenticationStateProvider in LoginNotify() the authentication against oracle database is implemented, with a ClaimsPrincipal and some static roles, to test the authorization in pages/components -> some pages should not be visible for specific users etc.

At this point my problems comes in, after the login of my user with valid credentials, I can´t see any created Cookie, this means after a Browser Refresh (F5), I always get redirected to the login page again! I know in the sample application with Microsoft Identity example (IdentityDbContext), the services.AddIdentity() is the only difference, but it uses an complete in app database.

Startup.cs

 public void ConfigureServices(IServiceCollection services)
 {
                
     services.AddRazorPages();
     services.AddHttpContextAccessor();
     services.AddServerSideBlazor();
    
     services.AddDatabaseDeveloperPageExceptionFilter();
    
     services.AddAuthorizationCore();
     services.Configure<CookiePolicyOptions>(options =>
     {
         options.CheckConsentNeeded = context => true;
         options.MinimumSameSitePolicy = SameSiteMode.None;
     });
     services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();            
 }

In Configure() at the end:

app.UseAuthentication();
app.UseAuthorization();

Should I use the Microsoft Identity example (IdentityDbContext) with the in app DB? In general the users have already an oracle user acount and no Register page is necessary to create new users, thats another process. At other side, I neeed to store somewhere for each specific user his roles and map them to the ClaimsPrincipal. I would like to keep simple as possible, but I think I should mix some custom logic with the Identity example, can you help me and give some advice?


Here are more posts on this topic which I read and tried:

ASP.Net Core 5.0 Authentication and Authorization
https://stackoverflow.com/questions/65665953/asp-net-core-5-0-authentication-and-authorization

Custom Authentication in Blazor WebAssembly – Detailed
https://codewithmukesh.com/blog/authentication-in-blazor-webassembly/

Use cookie authentication without ASP.NET Core Identity
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-5.0

Customising ASP.Net Identity in Blazor server side
https://mvc.tech/blog/blazoridentityuser/


dotnet-aspnet-core-blazordotnet-aspnet-core-security
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

miwan2-msft avatar image
0 Votes"
miwan2-msft answered

Hi, @Patrick-4313

You could try register ServerAuthenticationStateProviderservice in DI(dependency Injection) service.

  public void ConfigureServices(IServiceCollection services)
 {
        //...
        builder.Services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
        builder.Services.AddAuthorizationCore();
        //...
 }


If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Best Regards,
Michael Wang







5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.