Пример #1
0
 /// <summary>
 /// Method to get a single <see cref="UserEntity"/>.
 /// </summary>
 /// <param name="op"><see cref="UserOptionsSelect"/> filters options for the query.</param>
 /// <returns>An <see cref="UserEntity"/> or null.</returns>
 public UserEntity SingleOrNull(UserOptionsSelect op)
 {
     using (Db.Context)
     {
         return(UserManager.Select(op));
     }
 }
Пример #2
0
        /// <summary>
        /// Method to get authenticate user.
        /// </summary>
        /// <returns></returns>
        protected UserEntity GetAuthUser()
        {
            if (IsAuth())
            {
                int sessionUserId = (int)HttpWebServerApplication.Session.Get(CookAuth["sid"], "UserId");

                UserOptionsSelect options = new UserOptionsSelect
                {
                    PrimaryKey   = sessionUserId,
                    Dependencies = new List <EnumEntitiesDependencies> {
                        EnumEntitiesDependencies.All
                    }
                };

                return(Database.Users.SingleOrNull(options));
            }

            return(null);
        }
Пример #3
0
        /// <summary>
        /// Method to authenticate an User.
        /// </summary>
        /// <returns>A formated web server data response.</returns>
        public WebServerResponseData Authentication(NameValueCollection post)
        {
            log.Info($"Api WebServer Authentication : post => {post}.");

            try
            {
                //NameValueCollection nvc = HttpUtility.ParseQueryString(post);
                NameValueCollection nvc = post;

                log.Debug($"Api WebServer Authentication : Email => {nvc["email"]}.");
                log.Debug($"Api WebServer Authentication : Password => {nvc["email"].MD5Hash()}.");

                UserOptionsSelect options = new UserOptionsSelect
                {
                    Email         = nvc["email"],
                    Password      = nvc["password"].MD5Hash(),
                    CheckPassword = true,
                    Dependencies  = new List <EnumEntitiesDependencies> {
                        EnumEntitiesDependencies.All
                    }
                };

                UserEntity user = null;
                using (SQLiteSvc.Db.Context)
                {
                    user = Database.Users.SingleOrNull(options);
                }

                if (user == null)
                {
                    log.Info(string.Format("Api User Authentication : User not found [{0}].", post));

                    return(ResponseNotAuth());
                }
                else
                {
                    // Generate sid
                    string sid = ("").GuidToBase64();

                    // Generate token
                    string stoken = ("").GuidToBase64();

                    Uri myUri = new Uri(Uri.AbsoluteUrl);

                    //Response.AddCookie("stoken", stoken);
                    Response.AddCookie("sid", sid + ":" + stoken, "/", myUri.Host);

                    HttpWebServerApplication.Session.Set(sid, "sid", stoken);
                    HttpWebServerApplication.Session.Set(sid, "UserId", user.UserId);

                    Content["Authentication"] = true;

                    return(ResponseContentToJson());
                }
            }
            catch (Exception e)
            {
                log.Error("Api User Authentication failed !", e);
                return(ResponseNotAuth());
            }
        }
Пример #4
0
 /// <summary>
 /// Method to get a single <see cref="UserEntity"/> asynchronously.
 /// </summary>
 /// <param name="op"><see cref="UserOptionsSelect"/> filters options for the query.</param>
 /// <returns>An <see cref="UserEntity"/> or null.</returns>
 public Task <UserEntity> SingleOrNullAsync(UserOptionsSelect op)
 => Task.Run(() => SingleOrNull(op));