Unable to authenticate as admin

Hello,

I have a problem with the authentication to my new Blazor Studio application.

I have created a basic application. I added a connection to a MSSQL database to this application. The connection to the database is done with Windows authentication. It works well. I succeeded in integrating the database to my new Blazor Studio application.

Afterwards, I added the authentication to my Blazor Studio application using the database connection mentioned above. Everything went well. No error messages.

However, it doesn't work when I launch the application and try to log in with the Admin credentials (user: admin, password: admin) as it was done with the previous Radzen Studio applications. I get no message in the log. The authentication simply fails.

Thanks in advance for your help

Hi @bgauvreau,

You can try logging in incognito mode. Sometimes there could be an authentication cookie left from a previous session. Also try debugging the Login method of the AccountController. It should contain code that explicitly checks for admin/admin.

The code to manage the admin user is present and the execution environment is set to 'Development'.

 if (env.EnvironmentName == "Development" && userName == "admin" && password == "admin")
            {
                var claims = new List<Claim>()
                {
                    new Claim(ClaimTypes.Name, "admin"),
                    new Claim(ClaimTypes.Email, "admin")
                };

                roleManager.Roles.ToList().ForEach(r => claims.Add(new Claim(ClaimTypes.Role, r.Name)));
                await signInManager.SignInWithClaimsAsync(new ApplicationUser { UserName = userName, Email = userName }, isPersistent: false, claims);

                return Redirect($"~/{redirectUrl}");
            }

image

On the other hand, I noticed that the MSSQL table dbo.AspAppUsers does not contain any user. The admin user was not created when setting up authentication in the Blazor Studio application.

Is there a simple way to create the admin user afterwards?

Thanks

An admin user is never created as it is a security risk. If you check the pasted code you would see that a DB query isn't made for the admin user. Please try debugging the application and step in the Login method to verify the code executes correctly.

Also try clearing your browser cache or logging in anonymous session (new private window).

Here is what I get when I debug with VS2022

The application crashes before entering the Login method

I used a new private window for the test

I also get this in the browser's console windows when I click the Login button:

And this in the Blazor studio's log window when I ru the app from RBS:

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [a].[Id], [a].[ConcurrencyStamp], [a].[Name], [a].[NormalizedName]
      FROM [AspNetRoles] AS [a]
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (2ms) [Parameters=[@__user_Id_0='?' (Size = 450)], CommandType='Text', CommandTimeout='30']
      SELECT [a].[Id], [a].[ClaimType], [a].[ClaimValue], [a].[UserId]
      FROM [AspNetUserClaims] AS [a]
      WHERE [a].[UserId] = @__user_Id_0
info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (2ms) [Parameters=[@__userId_0='?' (Size = 450)], CommandType='Text', CommandTimeout='30']
      SELECT [a0].[Name]
      FROM [AspNetUserRoles] AS [a]
      INNER JOIN [AspNetRoles] AS [a0] ON [a].[RoleId] = [a0].[Id]
      WHERE [a].[UserId] = @__userId_0
info: System.Net.Http.HttpClient.TestProject.LogicalHandler[100]
      Start processing HTTP request POST https://localhost:5001/Account/CurrentUser
info: System.Net.Http.HttpClient.TestProject.ClientHandler[100]
      Sending HTTP request POST https://localhost:5001/Account/CurrentUser
info: System.Net.Http.HttpClient.TestProject.LogicalHandler[100]
      Start processing HTTP request POST https://localhost:5001/Account/CurrentUser
info: System.Net.Http.HttpClient.TestProject.ClientHandler[100]
      Sending HTTP request POST https://localhost:5001/Account/CurrentUser
info: System.Net.Http.HttpClient.TestProject.LogicalHandler[100]
      Start processing HTTP request POST https://localhost:5001/Account/CurrentUser
info: System.Net.Http.HttpClient.TestProject.ClientHandler[100]
      Sending HTTP request POST https://localhost:5001/Account/CurrentUser

You can ignore the NavigationException. The fact that you see the Login page means that navigation was successful.

Okay. But in the debugger this error seems to make the VS2022 debugging process crash. Any other clue that might help me solve the problem?

Can you clarify what that means? Do you see incorrect use or password? Do you get redirected to the index page? There are no known issues with the default user so I can't tell what could have gone wrong.

With regards to debugging you can just attach the debugger to the running process after seeing the Login page. Run the application with dotnet run then in Visual Studio pick Attach to process from the Debug menu and put a breakpoint in the Login method.

Hello,

I succeeded in debugging the Login method. In the method the username is well ' admin ' and the password is well ' admin '. But the redirectUrl parameter is 'null'.

When I press the 'Login' button on the login page, the login page reloads in loop. Is it possible that the 'redirectUrl' parameter at 'null' is the cause of this.

Thanks

No, if it is null the login will redirect to ~/ which is the start page of your application - the one whose url is set to /:

@page "/"

Okay. I checked and '/' does refer to the 'index' page in my code. In this case, do you have any idea why I am always redirected to the 'Login' page? I must admit that this is a bit beyond me.

It is beyond me too. The only time I've seen this happen was due to caching. Logging with a different browser or private window usually fixes it. You can try creating a new application with a different name to see if it makes a difference (the application name is used by ASP.NET session cookie).

Hello,

  • I tested 2 different applications
  • I used 2 different browsers with private browser windows
  • I tried 2 different databases (MSSQL and SQLite)
  • I made sure to delete all existing cookies

And I always get the same result.

Have you been able to test on your side. Could this be a bug?

Thanks

I've tested it multiple times and on multiple machines and it seems to work fine. There is no known bug that could manifest like this.

Here is a quick video.
login

1 Like

Hello,

It works on my side too now.

When I started running my application in VS2022, it asked me to auto-authenticate the SSL certificate used by the application. The address of the application is httpS://localhost:5001

Curiously, while running the application with Radzen Studio, it never happened that I was asked to auto-authenticate the SSL certificate.

Perhaps it would be worthwhile to investigate this behavior of Radzen.

Thanks for taking the time to support me with this problem.

Have a nice day :slight_smile:

Radzen Blazor Studio uses the dotnet run command to start the application. Nothing special really.

OK.

Then it is undoubtedly related to the configuration of the computer used for development.

Thank you. Have a nice day :slight_smile:

I investigated the same problem (blazor Server, deploy on IIS) and I think there is no solution yet.
The generated class AccountController uses the field User.
This field is declared in the base class Controller or ControllerBase as:
public ClaimsPrincipal User => HttpContext?.User!;
It uses HttpContext and that doesn't work in Blazer Server, see:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-context?view=aspnetcore-5.0#blazor-and-shared-state

While HttpContext doesn't work in Blazor it definitely works in controllers which is where Radzen application use it. The problem you are experiencing is not related to that as we have been using this approach for as long as Blazor has existed. Check if your application runs in HTTPS (it is a mandatory requirement) and that your certificate is valid and trusted.

Unfortunately, even after creating a new Radzen Blazor Studio project, I have the same problem.
In >Visual Studio >Run everything is OK. When deploying to IIS, after entering correct login data, the login page is displayed again, other pages are not accessible. The application is running in HTTPS.
Maybe you can help. The two log files are attached. Differences:
Test8-stage-20230109.log line 34: Authorization failed..
Test8-develop-20230109.log line 34: Request starting HTTP/1.1 POST https://localhost:5001/Account/CurrentUser - 0
Logfiles.zip (3.1 KB)