Esempio n. 1
0
        public async Task <IHttpActionResult> PostClient(ClientPostDto client)
        {
            var user = await UserManager.FindByEmailAsync(client.Username);

            if (user == null)
            {
                CustomException.ThrowNotFoundException($"User: {client.Username} doesn't exist.");
            }

            var       messageToSend = "Username: "******"Please provide origin for JavaScript web application.");
                }

                if (client.AllowedOrigin.Equals("*"))
                {
                    CustomException.ThrowBadRequestException("Sorry we cannot allow unlimited origin. Please provide direct domain address.");
                }

                newClient = await ClientService.AddAsync(client);

                messageToSend += "<br>" + "client_id: " + newClient.Id;
            }
            else
            {
                var clientSecret = ClientService.GenerateClientSecret();

                client.ClientSecret  = clientSecret;
                client.AllowedOrigin = "*";

                newClient = await ClientService.AddAsync(client);

                messageToSend         += "<br>" + "client_id: " + newClient.Id + "<br>" + "client_secret: " + clientSecret;
                newClient.ClientSecret = clientSecret;
            }


            await UserManager.SendEmailAsync(user?.Id, "New client", $"{messageToSend}");

            return(CreatedAtRoute("ClientRoute", new { id = newClient.Id }, newClient));
        }
Esempio n. 2
0
        public async Task <IHttpActionResult> AddClient(AddClientDto addClientModel)
        {
            var username = User.Identity.Name;
            var user     = await UserManager.FindByEmailAsync(username);

            var adminRoleId = RoleManager.Roles.SingleOrDefault(x => x.Name.Equals("Administrators"))?.Id;

            if (!user.Roles.Any(x => x.RoleId.Equals(adminRoleId)))
            {
                var jsClientCount = await ClientService.GetActiveJsClientCountByUserName(username);

                var nativeClientCount = await ClientService.GetActiveNativeClientCountByUserName(username);

                if (jsClientCount > 5)
                {
                    CustomException.ThrowBadRequestException("Only 5 JavaScript clients per user.");
                }

                if (nativeClientCount > 5)
                {
                    CustomException.ThrowBadRequestException("Only 5 native clients per user.");
                }
            }

            var client = new ClientPostDto
            {
                Username             = user.UserName,
                Active               = true,
                RefreshTokenLifeTime = 10080
            };

            var       messageToSend = "Username: "******"Please provide origin for JavaScript web application.");
                }

                if (addClientModel.AllowedOrigin.Equals("*"))
                {
                    CustomException.ThrowBadRequestException("Sorry we cannot allow unlimited origin. Please provide direct domain address.");
                }

                client.ApplicationType = 0;
                client.AllowedOrigin   = addClientModel.AllowedOrigin;

                newClient = await ClientService.AddAsync(client);

                messageToSend += "<br>" + "client_id: " + newClient.Id;
            }
            else
            {
                var clientSecret = ClientService.GenerateClientSecret();

                client.ClientSecret    = clientSecret;
                client.ApplicationType = 1;
                client.AllowedOrigin   = "*";

                newClient = await ClientService.AddAsync(client);

                newClient.ClientSecret = clientSecret;
                messageToSend         += "<br>" + "client_id: " + newClient.Id + "<br>" + "client_secret: " + clientSecret;
            }

            await UserManager.SendEmailAsync(user.Id, "New client", $"{messageToSend}");

            return(CreatedAtRoute("GetMyClientsRoute", new { id = newClient.Id }, newClient));
        }