Код: Выделить всё
using Microsoft.AspNetCore.Components;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace BadgerWatchWeb.Services
{
public class ServerAuthenticationStateProvider : AuthenticationStateProvider
{
string UserId;
string Password;
bool IsAuthenticated = false;
public void LoadUser(string _UserId, string _Password)
{
UserId = _UserId;
Password = _Password;
}
public async Task LoadUserData()
{
var securityService = new SharedServiceLogic.Security();
try
{
var passwordCheck = await securityService.ValidatePassword(UserId, Password);
IsAuthenticated = passwordCheck == true ? true : false;
} catch(Exception ex)
{
Console.WriteLine(ex);
}
}
public override async Task GetAuthenticationStateAsync()
{
var userService = new UserService();
var identity = IsAuthenticated
? new ClaimsIdentity(await userService.GetClaims(UserId))
: new ClaimsIdentity();
var result = new AuthenticationState(new ClaimsPrincipal(identity));
return result;
}
}
Мои службы настройки выглядят так:
Код: Выделить всё
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton();
services.AddScoped();
services.AddAuthorizationCore();
}
Когда я запускаю код, который я получаю InvalidOperationException: невозможно предоставить значение для свойства AuthenticationStateProvider для типа BadgerWatchWeb.Pages.Index. Не существует зарегистрированной службы типа BadgerWatchWeb.Services.ServerAuthenticationStateProvider.
Подробнее здесь: https://stackoverflow.com/questions/575 ... ce-of-type