示例#1
0
 public async Task <bool> ValidateAsync(string apiKey)
 {
     return(await Task.Run <bool>(() =>
     {
         return JWTokenHelper.ValidateToken(apiKey);
     }));
 }
示例#2
0
        private void ExtractUserInfo(string authHeader)
        {
            var token = authHeader.Split(' ')[1];                // gets the token from the header

            var dictionary = JWTokenHelper.DecodeJWToken(token); //

            int tmp;

            CurrentIdentity = new CurrentIdentity
            {
                Email    = dictionary["Email"].ToString(),
                Username = dictionary["Username"].ToString(),
                UserId   = int.TryParse(dictionary["UserId"].ToString(), out tmp) ? tmp : (int?)null
            };
        }
示例#3
0
        public HttpResponseMessage Authenticate(UserSerializer userSerializer)
        {
            try
            {
                var response = new List <object>();

                var foundUser =
                    Entities.Users.SingleOrDefault(u => u.Email == userSerializer.Username ||
                                                   u.Username == userSerializer.Username);
                if (foundUser == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, new ResponseMessage
                    {
                        Success = false,
                        Message = "Username not found."
                    }));
                }

                if (!BCrypt.Net.BCrypt.Verify(userSerializer.Password, foundUser.Password))
                {
                    return(Request.CreateResponse(HttpStatusCode.NotFound, new ResponseMessage
                    {
                        Success = false,
                        Message = "The password does not match."
                    }));
                }
                var tags = Entities.UserTagsGet(foundUser.Id).ToList();

                var t = new List <Models.Tag>();
                foreach (var tag in tags)
                {
                    t.Add(new Models.Tag
                    {
                        TagId     = tag.TagId,
                        Name      = tag.Name,
                        CreatedAt = tag.CreatedAt,
                        CreatedBy = tag.CreatedBy
                    });
                }

                Models.User user = new Models.User
                {
                    TotalFollowers = Entities.Follows.Count(f => f.FollowedId == foundUser.Id),
                    TotalPosts     = Entities.Posts.Count(p => p.UserId == foundUser.Id && p.TypeId != 3),
                    TotalBlogs     = Entities.Posts.Count(p => p.UserId == foundUser.Id && p.TypeId == 3),
                    UserTags       = new List <Tag>(t)
                };

                foundUser.CopyProperties <Data.User, Models.User>(user);



                var jwt = JWTokenHelper.GenerateJWToken(user);

                response.Add(user);
                response.Add(jwt);

                return(Request.CreateResponse(HttpStatusCode.OK, new ResponseMessage
                {
                    Success = true,
                    Message = "User authenticated.",
                    Data = response
                }));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, new ResponseMessage
                {
                    Success = false,
                    Message = ex.Message
                }));
            }
        }