Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.AspNetCore.Http;
- using OpenIddict.Abstractions;
- using OpenIddict.Server;
- using System.Net;
- namespace Company.WebApi.CustomToken;
- public class CustomTokenEndpointHandler(IHttpContextAccessor httpContextAccessor)
- : IOpenIddictServerHandler<OpenIddictServerEvents.ApplyTokenResponseContext>
- {
- private readonly IHttpContextAccessor _httpContextAccessor = httpContextAccessor;
- public ValueTask HandleAsync(OpenIddictServerEvents.ApplyTokenResponseContext context)
- {
- if (context.Request != null)
- {
- var request = context.Request.ClientId;
- if (request != "logistic_petrus_react_client")
- {
- return ValueTask.CompletedTask;
- }
- }
- else
- {
- throw new NullReferenceException("Отстутствует Request");
- }
- var response = context.Transaction.Response;
- var refreshToken = response?.GetParameter(OpenIddictConstants.Parameters.RefreshToken)?.ToString();
- var accessToken = response?.GetParameter(OpenIddictConstants.Parameters.AccessToken)?.ToString();
- if (!string.IsNullOrEmpty(refreshToken) && !string.IsNullOrEmpty(accessToken))
- {
- if (_httpContextAccessor.HttpContext != null)
- {
- var cookieOptions = new CookieOptions
- {
- HttpOnly = true,
- Secure = true,
- //IsEssential = true,
- SameSite = SameSiteMode.None,
- Expires = DateTimeOffset.UtcNow.AddDays(14),
- Path = "/",
- };
- _httpContextAccessor.HttpContext.Response.Cookies.Append("refresh_token", refreshToken, cookieOptions);
- _httpContextAccessor.HttpContext.Response.Cookies.Append("access_token", accessToken, cookieOptions);
- Console.WriteLine(1);
- }
- else
- {
- throw new NullReferenceException("Отстутствует httpContext");
- }
- if (response != null)
- {
- response.RemoveParameter(OpenIddictConstants.Parameters.RefreshToken);
- response.RemoveParameter(OpenIddictConstants.Parameters.AccessToken);
- }
- else
- {
- throw new NullReferenceException("Отстутствует response");
- }
- }
- return ValueTask.CompletedTask;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement