public async Task <IActionResult> Create(FromFormAttribute formData)
        {
            var token = await _tokenService.GetAccessTokenAsync(User.XeroUserId());

            var connections = await _xeroClient.GetConnectionsAsync(token);

            var tenantId = connections[0].TenantId.ToString();

            Payment payment = new Payment()
            {
                Invoice = new Invoice()
                {
                    InvoiceNumber = "INV-0002"
                },
                Account = new Account()
                {
                    Code = "200"
                },
                Date   = DateTime.Now,
                Amount = 3
            };

            await _accountingApi.CreatePaymentAsync(token.AccessToken, tenantId, payment);

            return(RedirectToAction("Index", "Invoices"));
        }
        public async Task <IActionResult> Logout(FromFormAttribute form)
        {
            _logger.LogInformation("User {Name} logged out at {Time}.",
                                   User.Identity.Name, DateTime.UtcNow);

            #region snippet1
            await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);

            #endregion

            return(RedirectToAction("Index", "Home"));
        }
Example #3
0
        Service ProcessService(Type type)
        {
            bool enabled = true;

            Service service = new Service()
            {
                Name = type.Name.Replace("Api", "").Replace("Controller", ""),
            };

            DiscoveryAttribute discoveryAttr = type.GetCustomAttribute <DiscoveryAttribute>(true);

            if (discoveryAttr != null)
            {
                service.DiscoveryName = discoveryAttr.DiscoveryName;
                service.Dependency    = discoveryAttr.Dependency;
                enabled = discoveryAttr.Enabled;
            }

            if (enabled)
            {
                string hostAddress = _HostAddress + (_HostAddress.EndsWith("/") ? "" : "/");
                string routePrefix = string.Empty;

                RouteAttribute routePrefixAttr = type.GetCustomAttribute <RouteAttribute>(true);
                if (routePrefixAttr != null)
                {
                    routePrefix = routePrefixAttr.Template + (routePrefixAttr.Template.EndsWith("/") ? "" : "/");
                }

                List <Operation> operations = new List <Operation>();

                MethodInfo[] methods = type.GetMethods();
                foreach (MethodInfo methodInfo in methods)
                {
                    string method        = string.Empty;
                    string route         = string.Empty;
                    string discoveryName = string.Empty;
                    string dependency    = string.Empty;

                    // public method only declared in this class
                    if (methodInfo.IsPublic && methodInfo.DeclaringType.FullName == type.FullName)
                    {
                        object[] methodAttrs = methodInfo.GetCustomAttributes(false);
                        foreach (object methodAttr in methodAttrs)
                        {
                            Type methodAttrType = methodAttr.GetType();

                            Type[] httpAttrInterfaces = methodAttrType.GetInterfaces();
                            foreach (Type interfaceType in httpAttrInterfaces)
                            {
                                if (interfaceType.Equals(typeof(IActionHttpMethodProvider)))
                                {
                                    // found HTTP attribute
                                    method = methodAttrType.Name.Replace("Http", "").Replace("Attribute", "").ToUpper();
                                    route  = ((HttpMethodAttribute)methodAttr).Template;
                                    break;
                                }
                            }

                            if (methodAttrType.Equals(typeof(RouteAttribute)))
                            {
                                // process route and method arguments - Route attribute template overrides very attribute template
                                RouteAttribute routeAttr = methodAttr as RouteAttribute;
                                if (routeAttr != null)
                                {
                                    route = routeAttr.Template;
                                }
                            }

                            //if (methodInfo.Name == "Calc3")
                            //    System.Diagnostics.Debugger.Break();

                            if (!string.IsNullOrWhiteSpace(route))
                            {
                                ParameterInfo[] arguments = methodInfo.GetParameters();
                                if (arguments != null)
                                {
                                    List <ParameterInfo> parameters        = new List <ParameterInfo>();
                                    ParameterInfo        fromBodyParameter = null;
                                    foreach (ParameterInfo parameterInfo in arguments)
                                    {
                                        if (!parameterInfo.ParameterType.Equals(typeof(HttpRequestMessage)))
                                        {
                                            FromBodyAttribute fromBodyAttr = parameterInfo.GetCustomAttribute <FromBodyAttribute>(false);
                                            FromFormAttribute fromFormAttr = parameterInfo.GetCustomAttribute <FromFormAttribute>(false);
                                            if (fromBodyAttr == null && fromFormAttr == null)
                                            {
                                                // ensure parameter name is not already in the route
                                                if (route.IndexOf("{" + parameterInfo.Name + "}") == -1)
                                                {
                                                    // parameter is in method signature but not in route
                                                    parameters.Add(parameterInfo);
                                                }
                                            }
                                            else
                                            {
                                                fromBodyParameter = parameterInfo;
                                            }
                                        }
                                    }

                                    if (parameters.Count > 0)
                                    {
                                        int index = 0;
                                        foreach (ParameterInfo parameterInfo in parameters)
                                        {
                                            route += (index == 0 ? "?" : "&") + parameterInfo.Name + "={" + parameterInfo.Name + "}";
                                            index++;
                                        }
                                    }
                                }
                            }

                            if (methodAttrType.Equals(typeof(DiscoveryAttribute)))
                            {
                                DiscoveryAttribute opDiscAttr = methodAttr as DiscoveryAttribute;
                                if (opDiscAttr != null)
                                {
                                    discoveryName = opDiscAttr.DiscoveryName;
                                    dependency    = opDiscAttr.Dependency;
                                }
                            }
                        }

                        if (method == string.Empty)
                        {
                            method = "GET";
                        }

                        if (route.StartsWith("~/"))
                        {
                            route = route.Substring(2);
                        }
                        else
                        {
                            route = routePrefix + route;
                        }

                        Operation operation = new Operation()
                        {
                            Name          = methodInfo.Name,
                            Method        = method,
                            Route         = route,
                            DiscoveryName = discoveryName,
                            Dependency    = dependency
                        };

                        // if no discovery nane found, make one out of route
                        if (string.IsNullOrWhiteSpace(operation.DiscoveryName) && !string.IsNullOrWhiteSpace(operation.Route))
                        {
                            Uri    uri       = new Uri(this._HostAddress + operation.Route);
                            string localPath = uri.LocalPath;
                            if (localPath.IndexOf("?") > -1)
                            {
                                operation.DiscoveryName = localPath.Substring(1, uri.LocalPath.IndexOf("?") - 1);
                            }
                            else if (localPath.IndexOf("{") > -1)
                            {
                                operation.DiscoveryName = localPath.Substring(1, uri.LocalPath.IndexOf("{") - 1);
                            }
                            else
                            {
                                operation.DiscoveryName = localPath;
                            }

                            if (operation.DiscoveryName.StartsWith("/"))
                            {
                                operation.DiscoveryName = operation.DiscoveryName.Substring(1);
                            }

                            if (operation.DiscoveryName.EndsWith("/"))
                            {
                                operation.DiscoveryName = operation.DiscoveryName.Substring(0, operation.DiscoveryName.Length - 1);
                            }
                        }

                        operations.Add(operation);
                    }
                }

                service.Operations = operations;
            }
            else
            {
                service = null;
            }

            return(service);
        }