コード例 #1
0
        public async Task SendEmail(string userId, string?clientIp)
        {
            if (EmailSettings.Type == EmailSettings.EmailTypeDisabled)
            {
                // Email shouldn't be disabled, but if it is, we want to
                // fail this job so it retries
                throw new Exception("Email is disabled, failing job to it will retry");
            }
            var user = await Db.Get <User>(userId);

            if (user == null)
            {
                throw new EmailJobException("No User");
            }
            if (user.Email.PlainText == null)
            {
                logger.LogDebug($"Sending email to user {user.Id} who doesn't have an email address");
                return;
            }
            var oneTimeToken = new OneTimeToken(user.Id);

            Uri verifyUri = this.MakeEmailLink("confirm-email", oneTimeToken.UserId, oneTimeToken.GetUnhashedToken());

            await Db.Save(oneTimeToken);

            FillAttributes(user, verifyUri.ToString(), clientIp);
            await SendOneEmail(EmailType, Attributes);
        }
コード例 #2
0
        public override async Task LoadResource()
        {
            try
            {
                var token = await Context.GetDatabase().TokenForToken(oneTimeToken, ActiveSession) ?? null;

                if (token == null || !token.IsValid())
                {
                    throw new HttpError(HttpStatusCode.NotFound, BadVerificationResponse.InvalidToken);
                }
                OneTimeToken = token;
            }
            catch (HttpError httpError)
            {
                throw new HttpError(httpError.Status, BadVerificationResponse.InvalidToken);
            }

            try
            {
                user = await Load <User>(OneTimeToken.UserId) ?? throw new HttpError(HttpStatusCode.BadRequest,
                                                                                     BadVerificationResponse.UserNotFound);
            }
            catch (HttpError httpError)
            {
                throw new HttpError(httpError.Status, BadVerificationResponse.UserNotFound);
            }
        }
        public override async Task LoadResource()
        {
            try
            {
                var ott = await Context.GetDatabase().TokenForToken(oneTimeToken) ?? null;

                if (ott == null || !ott.IsValid())
                {
                    throw new HttpError(HttpStatusCode.NotFound, BadPasswordResetResponse.InvalidToken);
                }

                OneTimeToken = ott;
            }
            catch (HttpError httpError)
            {
                throw new HttpError(httpError.Status, BadPasswordResetResponse.InvalidToken);
            }


            User = await Load <User>(OneTimeToken.UserId) ??
                   throw new HttpError(HttpStatusCode.BadRequest, BadPasswordResetResponse.UserNotFound);

            try
            {
                usernameCredentials = await Load <UsernameCredential>(u => u.UserId == OneTimeToken.UserId);
            }
            catch (HttpError httpError)
            {
                throw new HttpError(httpError.Status, BadPasswordResetResponse.UserNotFound);
            }
        }
コード例 #4
0
        public async Task SendEmail(string userId, string?clientIp)
        {
            if (EmailSettings.Type == EmailSettings.EmailTypeDisabled)
            {
                // Email shouldn't be disabled, but if it is, we want to
                // fail this job so it retries
                throw new Exception("Email is disabled, failing job to it will retry");
            }

            var user = await Db.Get <User>(userId);

            if (user == null)
            {
                throw new EmailJobException("No User");
            }
            if (user.Email.PlainText == null)
            {
                logger.LogDebug($"Sending email to user {user.Id} who doesn't have an email address");
                return;
            }

            var oneTimeToken = new OneTimeToken(user.Id);
            var uri          = GetFrontEndUri("/password/reset", new Dictionary <string, string>()
            {
                { "token", oneTimeToken.GetUnhashedToken() }
            });
            await Db.Save(oneTimeToken);

            FillAttributes(user, uri.ToString(), clientIp);
            await SendOneEmail(EmailType, Attributes);
        }