/// <summary> /// Returns a value that indicates whether the specified <paramref name="testerDoerBody"/> was invoked as <typeparamref name="TSuccess"/>. /// </summary> /// <typeparam name="T1">The type of the first parameter of the method.</typeparam> /// <typeparam name="T2">The type of the second parameter of the method.</typeparam> /// <typeparam name="T3">The type of the third parameter of the method.</typeparam> /// <typeparam name="T4">The type of the fourth parameter of the method.</typeparam> /// <typeparam name="TResult">The type of the return value of the method that you explicitly have chosen to ignore.</typeparam> /// <typeparam name="TSuccess">The type of the return value that indicates success of the method.</typeparam> /// <param name="testerDoerBody">The tester function delegate to invoke.</param> /// <param name="arg1">The first parameter of the <paramref name="testerDoerBody"/>.</param> /// <param name="arg2">The second parameter of the <paramref name="testerDoerBody"/>.</param> /// <param name="arg3">The third parameter of the <paramref name="testerDoerBody"/>.</param> /// <param name="arg4">The fourth parameter of the <paramref name="testerDoerBody"/>.</param> /// <returns><c>true</c> if an instance of <typeparamref name="TResult"/> has been created; otherwise <c>false</c>.</returns> public static TSuccess IgnoreResult <T1, T2, T3, T4, TResult, TSuccess>(TesterFunc <T1, T2, T3, T4, TResult, TSuccess> testerDoerBody, T1 arg1, T2 arg2, T3 arg3, T4 arg4) { Validator.ThrowIfNull(testerDoerBody, nameof(testerDoerBody)); TResult ignore; return(testerDoerBody(arg1, arg2, arg3, arg4, out ignore)); }
/// <summary> /// Returns a value that indicates whether the specified <paramref name="testerDoerBody"/> was invoked as <typeparamref name="TSuccess"/>. /// </summary> /// <typeparam name="T">The type of the first parameter of the method.</typeparam> /// <typeparam name="TResult">The type of the return value of the method that you explicitly have chosen to ignore.</typeparam> /// <typeparam name="TSuccess">The type of the return value that indicates success of the method.</typeparam> /// <param name="testerDoerBody">The tester function delegate to invoke.</param> /// <param name="arg">The first parameter of the <paramref name="testerDoerBody"/>.</param> /// <returns><c>true</c> if an instance of <typeparamref name="TResult"/> has been created; otherwise <c>false</c>.</returns> public static TSuccess IgnoreResult <T, TResult, TSuccess>(TesterFunc <T, TResult, TSuccess> testerDoerBody, T arg) { Validator.ThrowIfNull(testerDoerBody, nameof(testerDoerBody)); TResult ignore; return(testerDoerBody(arg, out ignore)); }
/// <summary> /// Repetitively executes the specified <paramref name="faultSensitiveMethod"/> until the operation is successful, the amount of retry attempts has been reached, or a failed operation is not considered related to transient fault condition. /// </summary> /// <typeparam name="T1">The type of the first parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="T2">The type of the second parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="T3">The type of the third parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="T4">The type of the fourth parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="T5">The type of the fifth parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="TResult">The type of the out result value of the function delegate encapsulates <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="TSuccess">The type of the return value that indicates success of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <param name="faultSensitiveMethod">The fault sensitive function delegate that is invoked until an operation is successful, the amount of retry attempts has been reached, or a failed operation is not considered related to transient fault condition.</param> /// <param name="arg1">The first parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="arg2">The second parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="arg3">The third parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="arg4">The fourth parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="arg5">The fifth parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="result">The result of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="setup">The <see cref="TransientOperationOptions"/> which need to be configured.</param> /// <returns>The return value that indicates success of the function delegate <paramref name="faultSensitiveMethod"/>.</returns> public static TSuccess TryWithFunc <T1, T2, T3, T4, T5, TResult, TSuccess>(TesterFunc <T1, T2, T3, T4, T5, TResult, TSuccess> faultSensitiveMethod, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, out TResult result, Action <TransientOperationOptions> setup = null) { var factory = TesterFuncFactory.Create(faultSensitiveMethod, arg1, arg2, arg3, arg4, arg5); return(TryWithFuncCore(factory, out result, setup)); }
/// <summary> /// Repetitively executes the specified <paramref name="faultSensitiveMethod"/> until the operation is successful, the amount of retry attempts has been reached, or a failed operation is not considered related to transient fault condition. /// </summary> /// <typeparam name="T">The type of the parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="TResult">The type of the out result value of the function delegate encapsulates <paramref name="faultSensitiveMethod"/>.</typeparam> /// <typeparam name="TSuccess">The type of the return value that indicates success of the function delegate <paramref name="faultSensitiveMethod"/>.</typeparam> /// <param name="faultSensitiveMethod">The fault sensitive function delegate that is invoked until an operation is successful, the amount of retry attempts has been reached, or a failed operation is not considered related to transient fault condition.</param> /// <param name="arg">The parameter of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="result">The result of the function delegate <paramref name="faultSensitiveMethod"/>.</param> /// <param name="setup">The <see cref="TransientOperationOptions"/> which need to be configured.</param> /// <returns>The return value that indicates success of the function delegate <paramref name="faultSensitiveMethod"/>.</returns> public static TSuccess TryWithFunc <T, TResult, TSuccess>(TesterFunc <T, TResult, TSuccess> faultSensitiveMethod, T arg, out TResult result, Action <TransientOperationOptions> setup = null) { var factory = TesterFuncFactory.Create(faultSensitiveMethod, arg); return(TryWithFuncCore(factory, out result, setup)); }
private static bool TryGetPrincipal <T>(HttpContext context, Func <HttpContext, string, T> authorizationParser, TesterFunc <HttpContext, T, ClaimsPrincipal, bool> principalParser, out ClaimsPrincipal principal) { principal = null; string authorizationHeader = context.Request.Headers[HeaderNames.Authorization]; if (string.IsNullOrEmpty(authorizationHeader)) { return(false); } T credentials = authorizationParser(context, authorizationHeader); if (credentials != null && principalParser(context, credentials, out principal)) { return(true); } return(false); }
/// <summary> /// Provides a generic way to make authentication requests using the specified <paramref name="context"/>. /// </summary> /// <typeparam name="T">The type of the credentials returned from <paramref name="authorizationParser"/> and passed to <paramref name="principalParser"/>.</typeparam> /// <param name="context">The context of the ASP.NET application.</param> /// <param name="requireSecureConnection">When <c>true</c>, the HTTP connection is required to use secure sockets (that is, HTTPS); when <c>false</c> no requirement is enforced.</param> /// <param name="authorizationParser">The function delegate that will parse the authorization header of a web request and return the credentials of <typeparamref name="T"/>.</param> /// <param name="principalParser">The function delegate that will parse the credentials of <typeparamref name="T"/> returned from <paramref name="authorizationParser"/> and if successful returns a <see cref="ClaimsPrincipal"/> object.</param> /// <exception cref="SecurityException"> /// Authorized failed for the request. /// </exception> public static void Authenticate <T>(HttpContext context, bool requireSecureConnection, Func <HttpContext, string, T> authorizationParser, TesterFunc <HttpContext, T, ClaimsPrincipal, bool> principalParser) { Validator.ThrowIfNull(context, nameof(context)); if (requireSecureConnection && !context.Request.IsHttps) { throw new SecurityException("An SSL connection is required for the request."); } ClaimsPrincipal principal; if (TryGetPrincipal(context, authorizationParser, principalParser, out principal)) { context.User = principal; return; } throw new SecurityException("Authentication failed for the request."); }
/// <summary> /// Provides a generic way to make authentication requests using the specified <paramref name="context"/>. /// </summary> /// <typeparam name="T">The type of the credentials returned from <paramref name="authorizationParser"/> and passed to <paramref name="principalParser"/>.</typeparam> /// <param name="context">The context of the ASP.NET application.</param> /// <param name="requireSecureConnection">When <c>true</c>, the HTTP connection is required to use secure sockets (that is, HTTPS); when <c>false</c> no requirement is enforced.</param> /// <param name="authorizationParser">The function delegate that will parse the authorization header of a web request and return the credentials of <typeparamref name="T"/>.</param> /// <param name="principalParser">The function delegate that will parse the credentials of <typeparamref name="T"/> returned from <paramref name="authorizationParser"/> and if successful returns a <see cref="ClaimsPrincipal"/> object.</param> /// <returns><c>true</c> if the specified parameters triggers a successful authentication; otherwise, <c>false</c>.</returns> public static bool TryAuthenticate <T>(HttpContext context, bool requireSecureConnection, Func <HttpContext, string, T> authorizationParser, TesterFunc <HttpContext, T, ClaimsPrincipal, bool> principalParser) { try { Authenticate(context, requireSecureConnection, authorizationParser, principalParser); return(true); } catch (SecurityException) { return(false); } }