/
githubmirror
/
aspnetcore
Обзор
Документация
Войти
/
githubmirror
/
aspnetcore
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/Security/Authentication/test/TestHandlers.cs
114 строк
3 KB
Chris Ross
Obsolete and replace ISystemClock in Security, Identity #47472 (#47717)
26 апр 2023, 05:02
Не верифицирован
26 апр 2023, 05:02
726f467
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Security.Claims; using System.Text.Encodings.Web; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace Microsoft.AspNetCore.Authentication.Tests; public class TestAuthHandler : AuthenticationHandler<AuthenticationSchemeOptions>, IAuthenticationSignInHandler { public TestAuthHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder) : base(options, logger, encoder) { } public int SignInCount { get; set; } public int SignOutCount { get; set; } public int ForbidCount { get; set; } public int ChallengeCount { get; set; } public int AuthenticateCount { get; set; } protected override Task HandleChallengeAsync(AuthenticationProperties properties) { ChallengeCount++; return Task.CompletedTask; } protected override Task HandleForbiddenAsync(AuthenticationProperties properties) { ForbidCount++; return Task.CompletedTask; } protected override Task<AuthenticateResult> HandleAuthenticateAsync() { AuthenticateCount++; var principal = new ClaimsPrincipal(); var id = new ClaimsIdentity(); id.AddClaim(new Claim(ClaimTypes.NameIdentifier, Scheme.Name, ClaimValueTypes.String, Scheme.Name)); principal.AddIdentity(id); return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(principal, new AuthenticationProperties(), Scheme.Name))); } public Task SignInAsync(ClaimsPrincipal user, AuthenticationProperties properties) { SignInCount++; return Task.CompletedTask; } public Task SignOutAsync(AuthenticationProperties properties) { SignOutCount++; return Task.CompletedTask; } } public class TestHandler : IAuthenticationSignInHandler { public AuthenticationScheme Scheme { get; set; } public int SignInCount { get; set; } public int SignOutCount { get; set; } public int ForbidCount { get; set; } public int ChallengeCount { get; set; } public int AuthenticateCount { get; set; } public Task<AuthenticateResult> AuthenticateAsync() { AuthenticateCount++; var principal = new ClaimsPrincipal(); var id = new ClaimsIdentity(); id.AddClaim(new Claim(ClaimTypes.NameIdentifier, Scheme.Name, ClaimValueTypes.String, Scheme.Name)); principal.AddIdentity(id); return Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(principal, new AuthenticationProperties(), Scheme.Name))); } public Task ChallengeAsync(AuthenticationProperties properties) { ChallengeCount++; return Task.CompletedTask; } public Task ForbidAsync(AuthenticationProperties properties) { ForbidCount++; return Task.CompletedTask; } public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context) { Scheme = scheme; return Task.CompletedTask; } public Task SignInAsync(ClaimsPrincipal user, AuthenticationProperties properties) { SignInCount++; return Task.CompletedTask; } public Task SignOutAsync(AuthenticationProperties properties) { SignOutCount++; return Task.CompletedTask; } } public class TestHandler2 : TestHandler { } public class TestHandler3 : TestHandler { }