5

I have a table created by Identity as AspNetUser.

I want to send an email to corresponding user if he did not update his password for six months. I have to send mail after 60 days and after that every 3 days. I am using .net framework 4.5.2, asp.net, mvc 5, identity2
My model

    public class IdentityUser : IUser
    {
        public IdentityUser();
        public IdentityUser(string userName);
        .
        .
        public DateTime LastUpdateDate{get;set;}
    }

My controller

public ActionResult PasswordSendMail()
{
    my code for checking six month...
}

But the mail has to be sent once at the 60th day from the LastUpdateDate and after every three days.
If i placed the code in controller, the action should be called. I have to send without calling any controller specifically.
I want to sent mail to users, whether user is logged in or not. when the time reached the mail should be sent by background process.
how to integrate the windows service with the current project. Brief answer will be helpful
Thanks in advance

anand
  • 1,559
  • 5
  • 21
  • 45
  • What have you already tried ? What errors are you getting ? – MrDeveloper Feb 22 '16 at 12:38
  • i have created the table. but cannot check when the password last updated – anand Feb 22 '16 at 12:47
  • Add a new property to the `IdentityUser` model? Call it `LastUpdatedPassword` which is a `DateTime` type? – Jamie Rees Feb 22 '16 at 12:48
  • As chris mentioned the best option is notify user during login like most banking site do. Else try backend job to send emails based on lastupdatedpassword. You can use Hangfire for the same. – jitendra singh Feb 24 '16 at 19:14
  • If you publish your application to IIS I recommend to use [Quartz.NET](http://www.quartz-scheduler.net/) with [Keep Alive Service For IIS 6.0/7.5](https://keepalive.codeplex.com/) as explained my answer on [execute a schedule with quartz in visual start now with interval 24 hours](http://stackoverflow.com/questions/35065229/execute-a-schedule-with-quartz-in-visual-start-now-with-interval-24-hours/35071844#comment57880149_35071844). Hope this helps... – Murat Yıldız Feb 25 '16 at 18:07
  • Hangfire is an easy way to perform fire-and-forget, delayed and recurring tasks in ASP.NET applications. Check - http://stackoverflow.com/questions/35127008/hangfire-implemetation and http://hangfire.io/ – jitendra singh Feb 28 '16 at 17:11

5 Answers5

6

You have two primary options here. The first and easiest is to simply not worry about notifying the user exactly at a certain interval, and just notify them upon login if it's time to change the password. You can simply add your code to check if the password is older than the defined timeframe to your login code, and then redirect them to a password change form if it's time. Or, if you really want to be fancy about it, you can create an action filter that does this check and redirects to the password change form, so the user will not be able to do anything else until they've updated their password.

If you really want to send an email on a schedule, then you need something that can run the code on a schedule. You have two options in this regard. First, you can create a console app that contains this code, and then simply use something like Task Scheduler in Windows to run it on a set schedule. Second, you can use something like Revalee to call your action on a schedule.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
1

If you want to schedule actions on your server, I would recommend to create a windows service with a timer that call a url at a given moment. I tried the task scheduler and it was not reliable enough for me.

This is the most important part of your windows service

protected override void OnStart(string[] args)
        {
            LaunchTimer(MilliSecondsToNextLaunch());
        }

protected void LaunchTimer(double interval)
        {
            Timer = new Timer { Interval = interval };
            Timer.Elapsed += OnTimer;
            Timer.Start();
        }

protected override void OnTimer(object sender, ElapsedEventArgs args)
        {
            using (var client = new WebClient())
            {
                client.DownloadString(siteUrl);
            }
            SetIntervalForTimer(MilliSecondsToNextLaunch() + 1000);
        }

protected void SetIntervalForTimer(double nextInterval)
        {
            Timer.Stop();
            Timer.Interval = nextInterval;
            Timer.Start();
        }
Daniel
  • 9,312
  • 3
  • 48
  • 48
  • the window service is inbuilt or have to install some external plugin – anand Feb 23 '16 at 08:53
  • You have to create the windows service (new project then) and deploy it on the server. It is very reliable and can restart with the server. – Daniel Feb 23 '16 at 09:14
  • a new project?, could i import it in the same project – anand Feb 23 '16 at 09:18
  • No. You have to create a service and install it on the server. – Daniel Feb 23 '16 at 10:50
  • can you explain in detail about window service – anand Feb 23 '16 at 13:45
  • @anand You can add a new windows service to your existing solution if you want to leverage any of your libraries. Once you compile the windows service you get a exe and all depedent DLL's. You will need to put these in a folder on the server and then run command installutil /i "Path to Exe", and it would install the service and would get it running as well. There are lot of options like when to start a service, under which account you want to execute the service etc. you can make use of that. This has all the information -https://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx – Subhash Dike Feb 25 '16 at 19:36
0

You can do it by using the SQL Stored Procedure. Alter your Identity table by adding a new column LastUpdateDate.

In you Stored Procedure check for the date difference between last update date and current date, store the value in a integer variable, then use the IF-ELSE statement to check if the update value is greater than 6 months.

If true select the email addresses of those user.

Now read those email addresses in ASP.Net and send email to those users.

If you can send me the schema of your Identity table, I can help you with the code.

For any further assistance feel free to contact.

Junaid Sultan
  • 343
  • 1
  • 5
  • 17
0

We already developed solution for this requirement.

  1. On the asp.net or mvc web site, add a webservice as an interface to send password

  2. Develop a basic console program, add service reference made in step #1, execute the service api in order to send email

if using windows service:

  1. register the console program as windows service using NSSM
  2. add a scheduler task to start/stop the service of the console program in step #3. http://forums.asp.net/t/1758972.aspx?start+and+stop+windows+service+using+windows+task+scheduler

But windows service, imo, is not necessary. In our case , we just use windows task scheduler to start the console program, and the console program will then call the webservice and email is sent

RAY
  • 2,201
  • 2
  • 19
  • 18
0

Check this solution will resolve your Issue

  • Create Console Application in Visual C# Write your Custom logic to check your validation.

Saineshwar Bageri - MVP
  • 3,851
  • 7
  • 41
  • 47