Beispiel #1
0
        public static UserLoggedInModel GetLoggedInUser(string token)
        {
            var handler = new JwtSecurityTokenHandler();

            var PolicyList = handler.ReadJwtToken(token);

            UserLoggedInModel oUserLoggedInModel = new UserLoggedInModel();

            foreach (var item in PolicyList.Claims)
            {
                foreach (var prop in oUserLoggedInModel.GetType().GetProperties())
                {
                    if (prop.Name.ToLower() == item.Type.ToLower())
                    {
                        if (prop.PropertyType == typeof(System.Int32))
                        {
                            prop.SetValue(oUserLoggedInModel, (!string.IsNullOrEmpty(item.Value) ? Convert.ToInt32(item.Value) : 0));
                        }
                        else if (prop.PropertyType == typeof(System.String))
                        {
                            prop.SetValue(oUserLoggedInModel, item.Value);
                        }
                    }
                }
            }



            return(oUserLoggedInModel);
        }
Beispiel #2
0
        /// <summary>
        /// purpose : To Handle the token and user logged in values
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Invoke(HttpContext context)
        {
            try
            {
                if (!string.IsNullOrEmpty(context.GetTokenAsync("token").Result))
                {
                    if (string.IsNullOrEmpty(context.Session.GetString(UserHelper.LoggedInUserEmailAddress)))
                    {
                        UserLoggedInModel oUserLoggedInModel = UserHelper.GetLoggedInUser(context.GetTokenAsync("token").Result);
                        context.Session.SetString(UserHelper.LoggedInUserEmailAddress, string.IsNullOrEmpty(oUserLoggedInModel.EmailAddress)?"": oUserLoggedInModel.EmailAddress);
                        context.Session.SetString(UserHelper.LoggedInUserName, string.IsNullOrEmpty(oUserLoggedInModel.FirstName + " " + Convert.ToString(oUserLoggedInModel.LastName))?"": oUserLoggedInModel.FirstName + " " + Convert.ToString(oUserLoggedInModel.LastName));
                        context.Session.SetInt32(UserHelper.LoggedInUserId, oUserLoggedInModel.UserId);
                    }
                }

                await _next(context);
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
        public IHttpActionResult Login(UserLoginModel userToLogin)
        {
            //UserValidator.ValidateAuthCode(userToLogin.AuthCode);
            //UserValidator.ValidateUsername(userToLogin.Username);

            var user = this.GetByUsernameAndAuthCode(userToLogin.Username, userToLogin.AuthCode);

            if (user == null)
            {
                return(this.BadRequest("Invalid user data."));
            }

            user.SessionKey = UserValidator.GenerateSessionKey(user.UserId);
            this.data.SaveChanges();

            var userLoggedModel = new UserLoggedInModel()
            {
                Username   = user.Username,
                SessionKey = user.SessionKey
            };

            return(Ok(userLoggedModel));
        }