private Task <ClaimsIdentity> GetIdentity(string username, string password)
 {
     using (var _context = new AdminDemoContext())
     {
         var data = _context.AdminLogins.Where(x => x.Username == username && x.Password == password && x.IsActive == true).FirstOrDefault();
         if (data != null)
         {
             return(Task.FromResult(new ClaimsIdentity(new GenericIdentity(username, "Token"), new Claim[] { })));
         }
     }
     // Credentials are invalid, or account doesn't exist
     return(Task.FromResult <ClaimsIdentity>(null));
 }
 public Country_Repository()
 {
     _context = new AdminDemoContext();
 }
 public Country_Repository(AdminDemoContext context)
 {
     _context = context;
 }
 public Currency_Repository()
 {
     _context = new AdminDemoContext();
 }
 public Currency_Repository(AdminDemoContext context)
 {
     _context = context;
 }
Exemplo n.º 6
0
        private async Task GenerateToken(HttpContext context)
        {
            try
            {
                string username = Convert.ToString(context.Request.Form["username"]);
                string password = Convert.ToString(context.Request.Form["password"]);
                string type     = Convert.ToString(context.Request.Form["type"]);
                // var identity = await _options.IdentityResolver(username, password);
                using (var _context = new AdminDemoContext())
                {
                    try
                    {
                        bool        data      = false;
                        int         id        = 0;
                        AdminLogins admindata = new AdminLogins();
                        if (type == "admin")
                        {
                            admindata = _context.AdminLogins.Where(x => x.Username == username && x.Password == password && x.IsActive == true).FirstOrDefault();
                            if (admindata != null)
                            {
                                data = true;
                                id   = admindata.AdminId;
                            }
                        }
                        if (data != false)
                        {
                            var identity = Task.FromResult(new ClaimsIdentity(new GenericIdentity(username, "Token"), new Claim[] { }));


                            var now    = DateTime.UtcNow;
                            var claims = new Claim[]
                            {
                                new Claim("type", type),
                                new Claim("id", id.ToString()),
                                new Claim(JwtRegisteredClaimNames.Sub, username),
                                new Claim(JwtRegisteredClaimNames.Jti, await _options.NonceGenerator()),
                                new Claim(JwtRegisteredClaimNames.Iat, new DateTimeOffset(now).ToUniversalTime().ToUnixTimeSeconds().ToString(), ClaimValueTypes.Integer64)
                            };

                            // Create the JWT and write it to a string
                            var jwt = new JwtSecurityToken(
                                issuer: _options.Issuer,
                                audience: _options.Audience,
                                claims: claims,
                                notBefore: now,
                                expires: now.Add(_options.Expiration),
                                signingCredentials: _options.SigningCredentials);
                            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

                            if (type == "admin")
                            {
                                var response = new
                                {
                                    status       = "success",
                                    access_token = encodedJwt,
                                    expires_in   = (int)_options.Expiration.TotalSeconds,
                                    firstname    = admindata.FirstName,
                                    lastname     = admindata.LastName,
                                    username     = admindata.Username,
                                    password     = admindata.Password,
                                    usertype     = type
                                };
                                // Serialize and return the response
                                context.Response.ContentType = "application/json";
                                context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                                await context.Response.WriteAsync(JsonConvert.SerializeObject(response, _serializerSettings));
                            }
                        }
                        else
                        {
                            context.Response.StatusCode = 200;
                            var response = new
                            {
                                status = "error",
                                msg    = "Invalid username or password."
                            };
                            context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                            await context.Response.WriteAsync(JsonConvert.SerializeObject(response, _serializerSettings));

                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
            catch (Exception ex) {
                throw ex;
            }
        }