Could you give me an sample of authentification?

Hello,

I am new to Blazor framework, can you give me a simple project with login authentification?


2 Replies

WR WANG RENJIE February 24, 2024 09:54 AM UTC

By the way, I'm using Blazor webapp.

Thanks.



AK Ashokkumar Karuppasamy Syncfusion Team February 26, 2024 02:33 PM UTC

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

Loader.
Up arrow icon