Ejemplo n.º 1
0
        public async void Register()
        {
            //repeat and success

            //Arrange
            var userForRegisterDto = new UserForRegisterDto {
                Username = "******", Password = "******"
            };
            var userToCreate = new User {
                Username = "******"
            };
            var mockRepo = new Mock <IAuthRepository>();

            mockRepo.Setup(repo => repo.UserExists(userForRegisterDto.Username))
            .ReturnsAsync(false);

            var setting = new TokenSetting {
                TokenKey = "Random super secret key hahaha"
            };
            var options = new Mock <IOptions <TokenSetting> >();

            options.Setup(x => x.Value).Returns(setting);

            var controller = new AuthController(mockRepo.Object, options.Object);
            //Act
            var result = await controller.Register(userForRegisterDto);

            //Assert
            Assert.IsType <StatusCodeResult>(result);
        }
 public TokenManagerService(IDistributedCache cache,
                            IHttpContextAccessor httpContextAccessor,
                            IOptions <TokenSetting> tokenSetting)
 {
     this._cache = cache;
     this._httpContextAccessor = httpContextAccessor;
     this._tokenSetting        = tokenSetting.Value;
 }
Ejemplo n.º 3
0
        public Utilities(MadpayDbContext dbContext, IConfiguration config, UserManager <User> userManager,
                         IHttpContextAccessor http)
        {
            _db          = dbContext;
            _config      = config;
            _userManager = userManager;
            var tokenSettingSection = _config.GetSection("TokenSetting");

            _tokenSetting = tokenSettingSection.Get <TokenSetting>();
            _http         = http;
        }
 public TokenController(IUnitOfWork <MadpayDbContext> dbContext, IAuthService authService,
                        IMapper mapper, ILogger <AuthController> logger, IUtilities utilities,
                        UserManager <Data.Models.User> userManager, SignInManager <Data.Models.User> signInManager, TokenSetting tokenSetting)
 {
     _db           = dbContext;
     _authService  = authService;
     _mapper       = mapper;
     _logger       = logger;
     _utilities    = utilities;
     _userManager  = userManager;
     _tokenSetting = tokenSetting;
 }
Ejemplo n.º 5
0
        public Utilities(Main_MadPayDbContext dbContext, IConfiguration config, UserManager <User> userManager,
                         IHttpContextAccessor http, ILookupClient lookupClient)
        {
            _db     = dbContext;
            _config = config;
            var tokenSettingSection = _config.GetSection("TokenSetting");

            _tokenSetting = tokenSettingSection.Get <TokenSetting>();
            _http         = http;
            _userManager  = userManager;
            _lookupClient = lookupClient;
        }
Ejemplo n.º 6
0
        public Utilitiess(IUnitOfWork <PayDelDbContext> dbContext, IConfiguration config, UserManager <Data.Models.User> userManager, IHttpContextAccessor http
                          , ILookupClient lookupClient)
        {
            _http        = http;
            _config      = config;
            _userManager = userManager;
            _userManager = userManager;
            _db          = dbContext;
            var tokenSettingSection = _config.GetSection("TokenSetting");

            _tokenSetting = tokenSettingSection.Get <TokenSetting>();
            _lookupClient = lookupClient;
        }
Ejemplo n.º 7
0
        public static string CreateJsonWebToken(TokenSetting settings, IEnumerable <Claim> claims)
        {
            var _claims = new List <Claim>
            {
                new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
            };

            _claims.AddRange(claims);

            var handler    = new JwtSecurityTokenHandler();
            var descriptor = new SecurityTokenDescriptor
            {
                Audience           = settings.AudienceId,
                Issuer             = settings.AudiencyIssuer,
                Subject            = new ClaimsIdentity(_claims),
                NotBefore          = DateTime.UtcNow,
                Expires            = DateTime.UtcNow.AddMinutes(Convert.ToInt32(settings.ExpireTime)),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(settings.AudienceSecret)), SecurityAlgorithms.HmacSha256Signature)
            };

            return(handler.WriteToken(handler.CreateToken(descriptor)));
        }
Ejemplo n.º 8
0
        public async void Login()
        {
            //Arrange
            var userForLoginDto = new UserForLoginDto {
                Username = "******", Password = "******"
            };
            var userFromRepo = new User {
                Username = "******"
            };
            var mockRepo = new Mock <IAuthRepository>();

            mockRepo.Setup(repo => repo.Login(userForLoginDto.Username, userForLoginDto.Password))
            .ReturnsAsync(userFromRepo);

            var mockRepo2 = new Mock <IAuthRepository>();

            mockRepo2.Setup(repo => repo.Login(userForLoginDto.Username, userForLoginDto.Password))
            .ReturnsAsync((User)null);


            var setting = new TokenSetting {
                TokenKey = "Random super secret key hahaha"
            };
            var options = new Mock <IOptions <TokenSetting> >();

            options.Setup(x => x.Value).Returns(setting);

            var controller  = new AuthController(mockRepo.Object, options.Object);
            var controller2 = new AuthController(mockRepo2.Object, options.Object);

            //Act
            var result = await controller.Login(userForLoginDto);

            var result2 = await controller2.Login(userForLoginDto);

            //Assert
            Assert.IsType <OkObjectResult>(result);
            Assert.IsType <UnauthorizedResult>(result2);
        }
Ejemplo n.º 9
0
 public TokenManager(IOptions <TokenSetting> tokenSetting)
 {
     _tokenSetting = tokenSetting.Value;
 }
Ejemplo n.º 10
0
 public AuthService(IOptions <TokenSetting> appSettings)
 {
     this._appSettings = appSettings.Value;
 }