示例#1
0
        /// <summary>
        /// Convert to string specified authentication trace
        /// </summary>
        /// <param name="authentication">Authentication</param>
        /// <returns>Returns string representing object</returns>
        public static string StringifyAuthentication(AuthenticationTrace authentication)
        {
            //Argumenta validation
            if (authentication == null)
            {
                throw new ArgumentNullException(nameof(authentication));
            }

            //Set authentication data or anonymous
            return(!authentication.IsAuthenticated ?
                   "authentication:Anonymous" : $"authentication:{authentication.AuthenticationType}->{authentication.IdentityName}");
        }
示例#2
0
        /// <summary>
        /// Executes generation of request trace using provided data
        /// </summary>
        /// <param name="httpMethod">Http method</param>
        /// <param name="controllerName">Controller name</param>
        /// <param name="actionName">Action name</param>
        /// <param name="actionParameters">Action parameters</param>
        /// <returns>Returns trace</returns>
        public static RequestTrace GenerateRequest(IPrincipal principal, string httpMethod, string controllerName, string actionName, IDictionary <string, object> actionParameters)
        {
            //Arguments validation
            if (principal == null)
            {
                throw new ArgumentNullException(nameof(principal));
            }
            if (string.IsNullOrEmpty(httpMethod))
            {
                throw new ArgumentNullException(nameof(httpMethod));
            }
            if (string.IsNullOrEmpty(controllerName))
            {
                throw new ArgumentNullException(nameof(controllerName));
            }
            if (string.IsNullOrEmpty(actionName))
            {
                throw new ArgumentNullException(nameof(actionName));
            }

            //Generate autentication trace holder
            AuthenticationTrace auth = new AuthenticationTrace();

            //With a valid authentication
            if (principal.Identity != null)
            {
                //Insert values on object
                auth.IsAuthenticated    = principal.Identity.IsAuthenticated;
                auth.AuthenticationType = principal.Identity.AuthenticationType;
                auth.IdentityName       = principal.Identity.Name;
            }

            //Initialize trace object
            return(new RequestTrace
            {
                //Set controller properties
                UniqueId = Guid.NewGuid(),
                CreationDate = DateTime.Now,
                HttpMethod = httpMethod,
                ControllerName = controllerName,
                ActionName = actionName,
                ActionParameters = actionParameters,

                //Set authentication
                Authentication = auth
            });
        }