Аутентификация компонентов бритвы - PullRequest
0 голосов
/ 29 марта 2019

Как использовать аутентификацию SignInAsync в качестве вызова от бритвенного компонента. Кажется, что выдает некоторые ошибки из-за того, что контекст находится в позднем состоянии конвейера.

Ошибка при вызове HttpContext.SignInAsync:

System.AggregateException: 'One or more errors occurred. (The response headers cannot be modified because the response has already started.)'    
InvalidOperationException: The response headers cannot be modified because the response has already started.

Вот UserState.cs, где реализован вход в систему.

public class UserState
{

    readonly IHttpContextAccessor Context;

    public UserState(IHttpContextAccessor Context)
    {
        this.Context = Context;
    }

    public string DisplayName { get; set; }

    public ClaimsPrincipal User => Context.HttpContext.User;

    public bool IsLoggedIn => User?.Identity.IsAuthenticated ?? false;

    public void SignIn()
    {
        var claims = new List<Claim> {
            new Claim(ClaimTypes.Name, "Bob", ClaimValueTypes.String),
            new Claim(ClaimTypes.Surname, "Builder", ClaimValueTypes.String),
        };
        var userIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
        var userPrincipal = new ClaimsPrincipal(userIdentity);

        Context.HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, userPrincipal,
            new AuthenticationProperties
            {
                ExpiresUtc = DateTime.UtcNow.AddMinutes(20),
                IsPersistent = true,
                AllowRefresh = true
            }).Wait();
    }
}

метод вызывается и вводится так:

@page "/"
@inject UserState User

@if (User.IsLoggedIn)
{
   <p>You are logged in</p>
} else {
  <p>You are NOT logged in</p>
}

<button onclick="@User.SignIn">Login</button>

Есть ли способ войти, получить ответ об успешном завершении, а затем обновить страницу с помощью метода SignIn.

Я видел похожую реализацию с использованием контроллеров MVC. Нет ли способа вызвать запрос аутентификации без участия mvc?

...