简体   繁体   中英

Custom blazor server-side authentication

I am new to blazor and stumbled across a problem when trying to implement authentication in my app. For now, I need to store all my users' data in JSON file and do not have a SQL DB to refer to, which, as I understand is needed to implement authentication.

As for now I have a list of users and check if the user is in my list of signed in accounts, then call markAsAuthenticated

public void markUserAsAuthenticated(string emailAddress)
    {
            var identity = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.Name, emailAddress),
            }, "someType"); 

            var user = new ClaimsPrincipal(identity);

            NotifyAuthenticationStateChanged (Task.FromResult(new AuthenticationState(user)));
    }

I would like to add roles or claims now to be able to include them in the component, but find it difficult to understand, what the following steps should be. Most of the tutorials use Authorization services provided by VisualStudio, but we work in VSCode and I would like to implement this myself, so I can use the list of users I have.

I would also like to find out if I can use cookies or JWT in my app and if there are any good tutorials for that or suggestions considering work with VSCode and server-side work, thanks in advance!

If you want to use cookies you need to use a razor page (.cshtml). You can have a razor page inside a Blazor application. It's the same code you'd use in a traditional razor application (pre-Blazor).

There's nothing Visual Studio specific to this that I know of.

See https://blazorhelpwebsite.com/ViewBlogPost/36

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM