/// <summary> /// Sets a property in the server transaction using the specified name and value. /// </summary> /// <typeparam name="TProperty">The type of the property.</typeparam> /// <param name="transaction">The server transaction.</param> /// <param name="name">The property name.</param> /// <param name="value">The property value.</param> /// <returns>The server transaction, so that calls can be easily chained.</returns> public static OpenIddictServerTransaction SetProperty <TProperty>( this OpenIddictServerTransaction transaction, string name, TProperty?value) where TProperty : class { if (transaction is null) { throw new ArgumentNullException(nameof(transaction)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentException(SR.GetResourceString(SR.ID0106), nameof(name)); } if (value is null) { transaction.Properties.Remove(name); } else { transaction.Properties[name] = value; } return(transaction); }
/// <summary> /// Retrieves the <see cref="IOwinRequest"/> instance stored in the <see cref="OpenIddictServerTransaction"/> properties. /// </summary> /// <param name="transaction">The transaction instance.</param> /// <returns>The <see cref="IOwinRequest"/> instance or <c>null</c> if it couldn't be found.</returns> public static IOwinRequest?GetOwinRequest(this OpenIddictServerTransaction transaction) { if (transaction is null) { throw new ArgumentNullException(nameof(transaction)); } if (!transaction.Properties.TryGetValue(typeof(IOwinRequest).FullName !, out object?property)) { return(null); } if (property is WeakReference <IOwinRequest> reference && reference.TryGetTarget(out IOwinRequest? request)) { return(request); } return(null); }
/// <summary> /// Retrieves the <see cref="HttpRequest"/> instance stored in the <see cref="OpenIddictServerTransaction"/> properties. /// </summary> /// <param name="transaction">The transaction instance.</param> /// <returns>The <see cref="HttpRequest"/> instance or <c>null</c> if it couldn't be found.</returns> public static HttpRequest GetHttpRequest([NotNull] this OpenIddictServerTransaction transaction) { if (transaction == null) { throw new ArgumentNullException(nameof(transaction)); } if (!transaction.Properties.TryGetValue(typeof(HttpRequest).FullName, out object property)) { return(null); } if (property is WeakReference <HttpRequest> reference && reference.TryGetTarget(out HttpRequest request)) { return(request); } return(null); }
/// <summary> /// Retrieves a property value from the server transaction using the specified name. /// </summary> /// <typeparam name="TProperty">The type of the property.</typeparam> /// <param name="transaction">The server transaction.</param> /// <param name="name">The property name.</param> /// <returns>The property value or <c>null</c> if it couldn't be found.</returns> public static TProperty?GetProperty <TProperty>( this OpenIddictServerTransaction transaction, string name) where TProperty : class { if (transaction is null) { throw new ArgumentNullException(nameof(transaction)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentException(SR.GetResourceString(SR.ID0106), nameof(name)); } if (transaction.Properties.TryGetValue(name, out var property) && property is TProperty result) { return(result); } return(null); }
public string GenerateAccessToken(IServiceProvider sp) { try { var claims = new List <Claim> { new Claim(Claims.Role, Roles.Administrator) }; var identity = new ClaimsIdentity(claims); var options = (IOptions <OpenIddictServerOptions>)sp .GetService(typeof(IOptions <OpenIddictServerOptions>)); var logger = (ILogger <OpenIddictServerDispatcher>)sp .GetService(typeof(ILogger <OpenIddictServerDispatcher>)); var transaction = new OpenIddictServerTransaction { Options = options.Value, Logger = logger }; var context = new ProcessSignInContext(transaction) { Issuer = new Uri("https://localhost:5001/"), AccessTokenPrincipal = new ClaimsPrincipal(identity) }; var generator = new GenerateIdentityModelAccessToken(); #pragma warning disable CA2012 generator.HandleAsync(context).GetAwaiter().GetResult(); #pragma warning restore CA2012 return(context.AccessToken); } catch (Exception) { throw; } }
/// <summary> /// Creates a new instance of the <see cref="BaseRequestContext"/> class. /// </summary> protected BaseRequestContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ProcessAuthenticationContext"/> class. /// </summary> public ProcessAuthenticationContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="BaseContext"/> class. /// </summary> protected BaseContext(OpenIddictServerTransaction transaction) => Transaction = transaction ?? throw new ArgumentNullException(nameof(transaction));
/// <summary> /// Creates a new instance of the <see cref="ProcessErrorContext"/> class. /// </summary> public ProcessErrorContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ExtractRevocationRequestContext"/> class. /// </summary> public ExtractRevocationRequestContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ExtractDeviceRequestContext"/> class. /// </summary> public ExtractDeviceRequestContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ExtractUserinfoRequestContext"/> class. /// </summary> public ExtractUserinfoRequestContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ProcessChallengeResponseContext"/> class. /// </summary> public ProcessChallengeResponseContext([NotNull] OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ValidateIntrospectionRequestContext"/> class. /// </summary> public ValidateIntrospectionRequestContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ExtractIntrospectionRequestContext"/> class. /// </summary> public ExtractIntrospectionRequestContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ValidateAuthorizationRequestContext"/> class. /// </summary> public ValidateAuthorizationRequestContext(OpenIddictServerTransaction transaction) : base(transaction) // Infer the redirect_uri from the value specified by the client application. => RedirectUri = Request?.RedirectUri;
/// <summary> /// Creates a new instance of the <see cref="ExtractAuthorizationRequestContext"/> class. /// </summary> public ExtractAuthorizationRequestContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ExtractLogoutRequestContext"/> class. /// </summary> public ExtractLogoutRequestContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="GenerateTokenContext"/> class. /// </summary> public GenerateTokenContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ValidateLogoutRequestContext"/> class. /// </summary> public ValidateLogoutRequestContext(OpenIddictServerTransaction transaction) : base(transaction) // Infer the post_logout_redirect_uri from the value specified by the client application. => PostLogoutRedirectUri = Request?.PostLogoutRedirectUri;
/// <summary> /// Creates a new instance of the <see cref="BaseValidatingContext"/> class. /// </summary> protected BaseValidatingContext([NotNull] OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ValidateDeviceRequestContext"/> class. /// </summary> public ValidateDeviceRequestContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ProcessSignoutResponseContext"/> class. /// </summary> public ProcessSignoutResponseContext([NotNull] OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ValidateUserinfoRequestContext"/> class. /// </summary> public ValidateUserinfoRequestContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="BaseValidatingTicketContext"/> class. /// </summary> protected BaseValidatingTicketContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ValidateConfigurationRequestContext"/> class. /// </summary> public ValidateConfigurationRequestContext(OpenIddictServerTransaction transaction) : base(transaction) { }
/// <summary> /// Creates a new instance of the <see cref="ProcessRequestContext"/> class. /// </summary> public ProcessRequestContext(OpenIddictServerTransaction transaction) : base(transaction) { }