Hi Wang Renjie,
We have created a sample in the Blazor web app (version 8.0). Refer to the
following blogs. The attached sample is provided for you to try. Please let us
know if you encounter any issues.
Blogs: https://www.syncfusion.com/blogs/post/easy-steps-create-a-blazor-server-app-with-authentication.aspx
Sample: Attached as Zip;
[CustomAuthStateProvider.cs]
|
using
Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;
using System.Threading.Tasks;
namespace
Authentication.Components.Account
{
public class CustomAuthStateProvider :
AuthenticationStateProvider
{
public override
Task<AuthenticationState> GetAuthenticationStateAsync()
{
var identity = new
ClaimsIdentity(new[]
{
new
Claim(ClaimTypes.Name, "exampleuser"),
}, "user authentication type");
var user = new
ClaimsPrincipal(identity);
return
Task.FromResult(new AuthenticationState(user));
}
}
}
|
[Auth.razor]
|
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
<PageTitle>Auth</PageTitle>
<h1>You are
authenticated</h1>
<AuthorizeView>
<Authorized>
<span> Hello,
@context.User.Identity.Name!</span>
<form method="post">
<button type="submit">Sign out</button>
</Form>
</Authorized>
<NotAuthorized>
<span> Welcome!
Please sign up below </span>
<a rel='nofollow' href="Employee/Register">Register</a>
<a rel='nofollow' href="Employee/Signin">Sign in</a>
</NotAuthorized>
</AuthorizeView>
|
Regards,
Ashok
Attachment:
Authentication_eaf8265f.zip