Esempio n. 1
0
        public async Task <IActionResult> RegistAsync([FromBody] LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (string.IsNullOrWhiteSpace(model.PWD) || string.IsNullOrWhiteSpace(model.PhoneNumber))
            {
                return(BadRequest());
            }

            IQueryable <mUser> query = _context.sUser.Where(x => x.PhoneNumber == model.PhoneNumber).Take(1);

            if (query.Count() > 0)
            {
                return(BadRequest());
            }


            var user = new mUser()
            {
                Password    = model.PWD,
                PhoneNumber = model.PhoneNumber
            };

            _context.sUser.Attach(user);
            await _context.SaveChangesAsync();

            return(Ok(new { Jwt = LocalJwt.Regist(_connectionMultiplexer.GetDatabase(), user.ID.ToString(), _configuration["jwt:Issuer"], int.Parse(_configuration["jwt:Overtime"])) }));
        }
Esempio n. 2
0
 public static AuthenticationBuilder AddLocalJwt(this AuthenticationBuilder builder, IConfiguration Configuration)
 {
     return(builder.AddJwtBearer(arg =>
     {
         arg.TokenValidationParameters = new TokenValidationParameters
         {
             ValidateIssuerSigningKey = true,
             IssuerSigningKey = LocalJwt.SecretKey,
             ValidateIssuer = true,
             ValidIssuer = Configuration["jwt:Issuer"],
             ValidateAudience = true,
             ValidateLifetime = true,
             ClockSkew = TimeSpan.Zero,
             AudienceValidator = (aud, key, token) =>
             {
                 bool res = true;
                 foreach (var aud_t in aud)
                 {
                     res &= LocalJwt.Check(RedisHolder.Instance[Configuration["redis:connect_string"]].GetDatabase(), aud_t);
                 }
                 return res;
             },
         };
     }));
 }
Esempio n. 3
0
        public IActionResult Verify([FromBody] LoginModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (string.IsNullOrWhiteSpace(model.PWD) || string.IsNullOrWhiteSpace(model.PhoneNumber))
            {
                return(BadRequest());
            }

            IQueryable <mUser> query = _context.sUser.Where(x => x.PhoneNumber == model.PhoneNumber).Where(x => x.Password == model.PWD).Take(1);
            var result = query.ToList();

            if (result.Count == 0)
            {
                return(NotFound());
            }

            return(Ok(new { Jwt = LocalJwt.Regist(_connectionMultiplexer.GetDatabase(), result[0].PhoneNumber.ToString(), _configuration["jwt:Issuer"], int.Parse(_configuration["jwt:Overtime"])) }));
        }