コード例 #1
0
        public OperationResponse <UserModelGeneralDto> GetById(Guid usuarioId)
        {
            OperationResponse <UserModelGeneralDto> response = new OperationResponse <UserModelGeneralDto>();

            try
            {
                var user = _serviceUser.GetUserById(usuarioId);

                var address = _serviceAddress.GetAddressByUserId(usuarioId);

                if (address != null)
                {
                    user.Address = address;
                }

                response.Data = user;
                response.Messages.Add(new OperationMessage {
                    Description = UserResource.User_Success_Search, Type = OperationMessageTypes.Success
                });
            }
            catch (Exception ex)
            {
                _serviceLog.Add(LogHelper.GenerateLog(ControllerContext, "GetById", UserResource.User_Error_Search, CurrentUser.Email, ex));

                response.Messages.Add(new OperationMessage {
                    Description = UserResource.User_Error_Search + " : " + ex.Message, Type = OperationMessageTypes.Error
                });
            }

            return(response);
        }
コード例 #2
0
        public async Task <OperationResponse <object> > Post(
            [FromBody] LoginRequestDto login,
            [FromServices] SigningConfigurations signingConfigurations,
            [FromServices] TokenConfigurations tokenConfigurations)
        {
            OperationResponse <object> response = new OperationResponse <object>();

            try
            {
                var userValid = _serviceLogin.Auth(login);

                if (userValid)
                {
                    ClaimsIdentity identity = new ClaimsIdentity(
                        new GenericIdentity(login.Email, "Login"),
                        new[] {
                        new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString("N")),
                        new Claim(JwtRegisteredClaimNames.UniqueName, login.Email),
                        new Claim(JwtRegisteredClaimNames.UniqueName, EncryptHelper.EncryptMD5(login.Password))
                    }
                        );

                    DateTime dataCriacao   = DateTime.Now;
                    DateTime dataExpiracao = dataCriacao +
                                             TimeSpan.FromSeconds(tokenConfigurations.Seconds);

                    var handler       = new JwtSecurityTokenHandler();
                    var securityToken = handler.CreateToken(new SecurityTokenDescriptor
                    {
                        Issuer             = tokenConfigurations.Issuer,
                        Audience           = tokenConfigurations.Audience,
                        SigningCredentials = signingConfigurations.SigningCredentials,
                        Subject            = identity,
                        NotBefore          = dataCriacao,
                        Expires            = dataExpiracao
                    });
                    var token = handler.WriteToken(securityToken);

                    var user = new
                    {
                        authenticated = true,
                        created       = dataCriacao.ToString("yyyy-MM-dd HH:mm:ss"),
                        expiration    = dataExpiracao.ToString("yyyy-MM-dd HH:mm:ss"),
                        accessToken   = token,
                        message       = "OK"
                    };

                    response.Data = user;
                    response.Messages.Add(new OperationMessage {
                        Description = LoginResource.Login_Sucessful, Type = OperationMessageTypes.Success
                    });
                    return(response);
                }

                response.Messages.Add(new OperationMessage()
                {
                    Description = LoginResource.Login_Error, Type = OperationMessageTypes.Error
                });
                return(response);
            }
            catch (Exception ex)
            {
                _serviceLog.Add(LogHelper.GenerateLog(ControllerContext, "Auth", LoginResource.Login_Error, "", ex));
                response.Messages.Add(new OperationMessage()
                {
                    Description = LoginResource.Login_Error, Type = OperationMessageTypes.Error
                });
                return(response);
            }
        }