The page using Microsoft.AspNetCore.Authorization is not displayed even when logged in

Dũng Nguyễn Văn 0 Reputation points
2023-10-23T08:32:44.31+00:00

Hi I have a problem in .Net MAUI Blazor like this. That is when I go to a page asking for authorize(notice-list) it redirects me to login form but after logging in and return to that page, the login interface will appear again. But when I go to a page that doesn't exist (Sorry, there's nothing at this address), then return to notice-list it displays the notice-list page interface. In short, when I logged in, it confirmed that there was a user and but notice-list still show login interface, only when I go to a page that doesn't exist and come back then it display interface. Does anyone have this problem before?

The Authorize I use in notice-list page.

@page "/notice-list"

@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]

The code I use for login button.

@code{
	private async Task Login()
	{
	    if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
	    {
	        errorMessage = "";
	        return;
	    }
	    var Token = SecurityManager.GenerateToken("nge", "p@ssw0rd", null, null, DateTime.Now.Ticks);
	
	    var uri = baseURI + "/Users/Login?UserName=" + userName + "&Password=" + password + "&Token=" + Token;
	    try
	    {
	        using (HttpClient httpClient = new HttpClient())
	        {

	            var response = await httpClient.GetStringAsync(uri);
	

	            if (string.IsNullOrEmpty(response) || response == "null")
	            {
	                errorMessage = "";
	            }
	            else
	            {
	                var cas = new CustomAuthenticationStateProvider();
	                await cas.Login(response);/
	
	                navigationManager.NavigateTo("/notice-list");
	            }
	        }
	    }
	    catch (Exception ex)
	    {
	        errorMessage = "error: " + ex.Message;
	    }
	}
}
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,500 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,231 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 39,391 Reputation points Microsoft Vendor
    2023-10-24T02:22:56.21+00:00

    Hello,

    The AuthorizeAttribute can not be applied to Razor Page handlers. For example, [Authorize] can't be applied to OnGet, OnPost, or any other page handler. Consider using an ASP.NET Core MVC controller for pages with different authorization requirements for different handlers. Using an MVC controller when different authorization requirements are required:

    For information on how to use authentication in Razor pages, please refer to Authorize attribute and Razor Pages for best practices.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    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.

    0 comments No comments