Esempio n. 1
0
        public async Task <IActionResult> RefreshToken([FromBody] UserRefreshTokenCommand authentication)
        {
            NotificationResult result = new NotificationResult();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(AppSettings.Site.UrlApi);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));

                var contentData = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("refresh_token", authentication.Refresh_token),
                    new KeyValuePair <string, string>("grant_type", "refresh_token")
                });

                var response = await client.PostAsync("connect/token", contentData);

                string str = await response.Content.ReadAsStringAsync();

                var tokenAuthentication = JsonConvert.DeserializeObject <UserTokenCommandResult>(str);

                if (response.IsSuccessStatusCode)
                {
                    result.Data = tokenAuthentication;
                }
                else
                {
                    result.AddError(tokenAuthentication.Error_description);
                }
            }

            return(Ok(result));
        }
Esempio n. 2
0
        public override async Task HandleTokenRequest(HandleTokenRequestContext context)
        {
            NotificationResult result = null;
            var userService           = GetUserService();

            if (context.Request.IsPasswordGrantType())
            {
                result = await userService.IsValidUsernameAndPasswordAsync(context.Request.Username, context.Request.Password);
            }
            else if (context.Request.IsRefreshTokenGrantType())
            {
                var    idUser   = new Guid(context.Ticket.Principal.GetClaim(ClaimTypes.NameIdentifier));
                string username = context.Ticket.Principal.GetClaim(ClaimTypes.Name);

                result = await userService.IsValidUsernameAndTokenAsync(username, idUser);
            }

            if (result.IsValid && result.Data == null && result.Messages.Any(x => x.Type == NotificationResult.NotificationMessageType.Warning))
            {
                result.AddError(result.Messages.First(x => x.Type == NotificationResult.NotificationMessageType.Warning).Message);
            }

            if (!result.IsValid)
            {
                context.Reject(
                    error: OpenIdConnectConstants.Errors.InvalidGrant,
                    description: result.GetErrors()
                    );
            }
            else
            {
                var identity = new ClaimsIdentity(OpenIdConnectServerDefaults.AuthenticationScheme);
                var user     = result.Data as UserCommandResult;

                identity.AddClaim(OpenIdConnectConstants.Claims.Subject, user.IdUser.ToString());
                identity.AddClaim(ClaimTypes.NameIdentifier, user.IdUser.ToString(), Destinations.AccessToken);
                identity.AddClaim(ClaimTypes.Name, user.Username, Destinations.AccessToken);
                identity.AddClaim(ClaimTypes.GivenName, user.FirstName, Destinations.AccessToken, Destinations.IdentityToken);
                identity.AddClaim(ClaimTypes.Email, user.Email, Destinations.AccessToken, Destinations.IdentityToken);

                var ticket = new AuthenticationTicket(
                    new ClaimsPrincipal(identity),
                    new AuthenticationProperties(),
                    OpenIdConnectServerDefaults.AuthenticationScheme
                    );

                ticket.SetScopes(
                    Scopes.OpenId,
                    Scopes.OfflineAccess
                    );

                context.Validate(ticket);
            }
        }
        public async Task <NotificationResult> DeleteByIdAsync(int id)
        {
            var result = new NotificationResult();

            try
            {
                await _uow.Connection.ExecuteAsync("delete from ShoplistIngredient where Id = @id", new { id }, _uow.Transaction);
            }
            catch (Exception ex)
            {
                result.AddError(ex);
            }

            return(result);
        }
        public async Task <NotificationResult> UpdateAsync(ShoplistIngredientInfo item)
        {
            var result = new NotificationResult();

            try
            {
                _context.Attach(item);
                _context.Entry(item).State = EntityState.Modified;
                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                result.AddError(ex);
            }

            return(result);
        }
        public async Task <NotificationResult> InsertAsync(ShoplistIngredientInfo item)
        {
            var result = new NotificationResult();

            try
            {
                await _context.AddAsync(item);

                item.SetId(_context.SaveChanges());
            }
            catch (Exception ex)
            {
                result.AddError(ex);
            }

            return(result);
        }
Esempio n. 6
0
        public async Task <NotificationResult> InsertAsync(ProductInfo product)
        {
            var result = new NotificationResult();

            try
            {
                await _context.Product.AddAsync(product);

                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                result.AddError(ex);
                throw;
            }
            return(result);
        }
Esempio n. 7
0
        public async Task <NotificationResult> ValidateUsernameAndPasswordAsync(string username, string password)
        {
            var result = new NotificationResult();
            var user   = await _userRepository.GetUserByLoginAsync(username);

            var hashedPassword = Cryptography.Hash(password);

            if (user.Password == hashedPassword)
            {
                result.IsValid = true;
                user.Password  = null;
                result.Data    = user;
            }
            else
            {
                result.IsValid = false;
                result.AddError("Invalid username/password");
            }
            return(result);
        }
Esempio n. 8
0
 public NotificationResult Commit(NotificationResult result)
 {
     if (result.IsValid)
     {
         try
         {
             _uow.Commit();
         }
         catch (Exception ex)
         {
             _uow.Rollback();
             result.AddError(ex);
         }
     }
     else
     {
         _uow.Rollback();
     }
     return(result);
 }
Esempio n. 9
0
        public async Task <IActionResult> Login([FromBody] UserAuthenticationCommand authentication)
        {
            var result = new NotificationResult();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(AppSettings.Site.UrlApi);
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
                client.DefaultRequestHeaders.AcceptLanguage.Add(new StringWithQualityHeaderValue(System.Threading.Thread.CurrentThread.CurrentCulture.Name));

                var contentData = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("username", authentication.Username),
                    new KeyValuePair <string, string>("password", authentication.Password),
                    new KeyValuePair <string, string>("grant_type", "password")
                });

                var response = await client.PostAsync("connect/token", contentData);

                var str = await response.Content.ReadAsStringAsync();

                var tokenAuthentication = JsonConvert.DeserializeObject <UserTokenCommandResult>(str);

                if (response.IsSuccessStatusCode)
                {
                    result.Data    = tokenAuthentication;
                    result.IsValid = true;
                }
                else
                {
                    result.AddError(tokenAuthentication.Error_description);
                }
            }

            return(Ok(result));
        }
Esempio n. 10
0
        public NotificationResult Commit(NotificationResult result)
        {
            if (result.IsValid)
            {
                try
                {
                    _uow.Commit();
                }
                catch (Exception ex)
                {
                    _uow.Rollback();
                    result.AddError(ex);
                }
            }
            else
            {
                _uow.Rollback();
            }

            // Removing the exceptions objects before returning the result.
            result.Errors.ToList().ForEach(x => x.Exception = null);

            return(result);
        }