Wednesday, March 2, 2016

SignOut from all opened tabs when SignOut from one window using Javascript LocalStorage events

It is one of the very important requirements in the web application development. Assume, you are opening an application in multiple browser tabs. When you do sign out in one window the authentication session will be cleared and gets signed out in that window. But all other opened window also should sign out automatically without any direct browser interaction. To do that,

keep the user's logged in state in a browser's LocalStorage. update it when you logged in or logged out. Utilize the LocalStorage events to track the status and do a refresh.

But How..? you can do it using Timer. But using local storage events it can be done without any client-side overhead. The below code will do that for you.
Initialize the below LocalStorage 'storage' event listener in the application's common load method.
window.addEventListener('storage', this.storageChange, false);

When you first Sign In, update the local storage variable isLoggedIn value to 'true'
localStorage.setItem('isLoggedIn', true);

when you sign out update the local storage variable isLoggedIn value to 'false'

and the callback method will have the logic to refresh the page based on the condition. That is, it will keep on checking the isLoggedIn value is 'false'  whenever you update the LocalStorage.
function storageChange(event) {

    if (event.key == 'isLoggedIn' && event.newValue == 'false') {

        window.location.reload();

    }

},

Wednesday, February 3, 2016

Remove HTML tags from string using JavaScript

I encountered a problem in Firefox that it doesn't support InnerText when I try to extract only text from an HTML.
    document.getElementById("mainDiv").InnerText
It works fine in chrome. So tried to remove the HTML tags from the InnerHTML string. Below is the solution for that.

    var html = document.getElementById("mainDiv");

    var text = html.replace(/<\S[^><]*>/g, '');

Setting up SMTP on IIS 7 to send mail from pickup directory

1. Start -> Administrative Tools -> Server manager

2. Go to Features, select "add features", Tick the "SMTP Server" (if it is not already installed), choose to install the required "Remote Server Administration Toos"

3. Go to Servies, Check to confirm that "Simple Mail Transfer Protocol (SMTP)" service is running, if not Start the service.



4. Start -> Administrative Tools -> Internet Information Services(iis) 6.0

5. Make sure that SMTP Virtual Server server is running, if not, right click, then choose "start"

6. Open SMTP Virtual Server properties by right clicking it.

7. On General tab, Set IP address of the Web server instead of "All Unassigned".

8. In Access tab, click on Relay button, this will open Relay Restrictions dialog.

9. In relay computers list, add the loopback IP address i.e 127.0.0.1 and IP address of the Web server, so that they can pass/relay emails through the SMTP server.

10. In IIS7,  go to website, double click "SMTP E-mail", Click on "Deliver e-mail to SMTP server", check the "Use localhost" checkmark

11. Your code should be like,

using (MailMessage message = new MailMessage())
{
    message.Subject = this.Subject;
    message.Body = this.Body;
    message.IsBodyHtml = this.BodyIsHtml;
    message.From = this.From;

    foreach (MailAddress address in this.To) message.To.Add(address);
    foreach (MailAddress address in this.CC) message.CC.Add(address);
    if (this.Bcc != null) message.Bcc.Add(this.Bcc);

    foreach (Attachment attachment in this.Attachments) message.Attachments.Add(attachment);

    SmtpClient smtp = new SmtpClient();
    smtp.Send(message);
}

12. And in Web.Config,

<system.net>
  <!-- Mail settings -->
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="C:\inetpub\mailroot\Pickup"/>
    </smtp>
  </mailSettings>
</system.net>

The above steps could solve the following common error you may encounter,
  • "Mailbox unavailable. The server response was: 5.7.1 Unable to relay for"