Example #1
0
        /// <summary>
        /// Attempts to log in a user.
        /// </summary>
        /// <param name="name">Username. Cannot be null.</param>
        /// <param name="pass">Password. Cannot be null.</param>
        /// <param name="hostname">Host name where the user is logging in from. Cannot be null.</param>
        /// <param name="delayFailedLogin">Whether failed login should cause artificial delay.</param>
        /// <returns>Login attempt result. Cannot be null.</returns>
        public LoginResult CheckAuthentication(string name, string pass, string hostname, bool delayFailedLogin)
        {
            if (string.IsNullOrWhiteSpace(name) || string.IsNullOrEmpty(pass))
            {
                return(LoginResult.CreateError(LoginError.InvalidPassword));
            }

            var lc = name.ToLowerInvariant();

            return(repository.HandleTransaction(ctx => {
                if (IsPoisoned(ctx, lc))
                {
                    ctx.AuditLogger.SysLog(string.Format("failed login from {0} - account is poisoned.", MakeGeoIpToolLink(hostname)), name);
                    return LoginResult.CreateError(LoginError.AccountPoisoned);
                }

                // Attempt to find user by either lowercase username.
                var user = ctx.Query().FirstOrDefault(u => u.Active && (u.NameLC == lc || (u.Options.EmailVerified && u.Email == name)));

                if (user == null)
                {
                    ctx.AuditLogger.AuditLog(string.Format("failed login from {0} - no user.", MakeGeoIpToolLink(hostname)), name);
                    if (delayFailedLogin)
                    {
                        Thread.Sleep(2000);
                    }
                    return LoginResult.CreateError(LoginError.NotFound);
                }

                // Attempt to verify password.
                var hashed = LoginManager.GetHashedPass(user.NameLC, pass, user.Salt);

                if (user.Password != hashed)
                {
                    ctx.AuditLogger.AuditLog(string.Format("failed login from {0} - wrong password.", MakeGeoIpToolLink(hostname)), name);
                    if (delayFailedLogin)
                    {
                        Thread.Sleep(2000);
                    }
                    return LoginResult.CreateError(LoginError.InvalidPassword);
                }

                // Login attempt successful.
                ctx.AuditLogger.AuditLog(string.Format("logged in from {0} with '{1}'.", MakeGeoIpToolLink(hostname), name), user);

                user.UpdateLastLogin(hostname);
                ctx.Update(user);

                return LoginResult.CreateSuccess(new UserContract(user));
            }));
        }
Example #2
0
        public static async Task <LoginResult> Login(string requestUserHostAddress, LoginContext loginContext)
        {
            try
            {
                var sessionToken = await Database.CreateGuestSfsSession(loginContext, requestUserHostAddress);

                return(LoginResult.CreateSuccess(new
                {
                    userLoginId = loginContext.GenerateAppScopedLoginId(),
                    sessionToken = sessionToken
                }));
            }
            catch (Exception e)
            {
                return(LoginResult.CreateFailed("Oops! Please try signing in again."));
            }
        }
        public static async Task <LoginResult> Login(string accessToken, string ipAddress, LoginContext loginContext)
        {
            try
            {
                var userLoginId = loginContext.GenerateAppScopedLoginId();
                EnsureAccessTokenNotEmpty(accessToken);
                var tokenDebugInfo = await Facebook.GetFacebookAccessTokenDebugInfo(accessToken);

                if (!tokenDebugInfo.IsValid() || tokenDebugInfo.user_id != loginContext.UserId)
                {
                    throw new Exception("Invalid Access Token. " + tokenDebugInfo.GetErrorMessage());
                }

                var sessionToken = await Database.CreateSfsSession(loginContext, ipAddress, accessToken);

                string tokenToReturn = accessToken;

                // not doing this for now. let the user renew his 2 hr token
                //if (tokenDebugInfo.IsTokenExpiringInLessThanOneDay())
                //{
                //    try
                //    {
                //        tokenToReturn = await Facebook.ExchangeTokenForLongLivedToken(accessToken);
                //        await Database.UpdateUserAccessToken(userLoginId, tokenToReturn);
                //    }
                //    catch (Exception e)
                //    {
                //        // do nothing let go as exchanging token or saving it is not important to stop user login flow. it can be done
                //        // at a later login.
                //    }
                //}

                return(LoginResult.CreateSuccess(new
                {
                    newAccessToken = tokenToReturn == accessToken ? null : tokenToReturn,
                    userLoginId = loginContext.GenerateAppScopedLoginId(),
                    sessionToken = sessionToken
                }));
            }
            catch (Exception e)
            {
                return(LoginResult.CreateFailed("Oops! Please try signing in again."));
            }
        }