Saturday, June 11, 2011

Opening window without menu and address bar

In the recent project I had to open the page without browser's menu bar, toolbar and also without address bar. So I tried following to achieve my goal...

Before the default page I put a html page and on its body onload called a user defined wininit() method which first opens the default page and after that closes the contained page.

wininit() method first opens the default page (the page we are willing to open) and again opens a blank page with target= _self and then closes it.

Here is our wininit() method...

function wininit()
{
var win = window.open("index.aspx", "HomeOpener", "status=0, toolbar=0, menubar=0, resizable=1, width=" + screen.availWidth + ", height = " + screen.availHeight + '"');
win.moveTo(0, 0);
window.open('', '_self', '');
window.close();
}

Here index.aspx is our default page.

Notice that we are opening a blank page before calling the window.close() method. It is done all because it could produce a warning message for closing the window.

Also notice that new window is open as maximized as its width and height is set as screen height and width.

Put the wininit() method in head block and call it on body onload.

Note: It works fine with IE and Google Chrome but in Mozilla Firefox it opens the default window as per our desire but does not close the container page.

Thursday, June 2, 2011

Remember Me option in login page

There is a popular option in login page i.e Remember me. If we check Remember me, it remembers login-id and password after logging out from the website. Also when Remember me is not checked, it work as a normal control... means it does not remember the login-id or password after exit the website.

When Remember me is checked, it simply saves the login credentials (of course valid one) into the browser in form of cookie.

So after successful login, credentials is written into cookie. Also after logout, cookie is not destroyed and is added to the http response. It is tracked at the login page and login-id and password is set.

After checking the credential call this method...

public void WriteCredentials() {

HttpCookie cookie = new HttpCookie("Login_Credentials");

if (chkRememberMe.Checked)
{
cookie.Values.Add("user_name", txtUserName.Text);
cookie.Values.Add("user_password", txtPassword.Text);

cookie.Expires.AddDays(30); //Cookie will expire after 30 days.
}
else
{
cookie.Values.Add("user_name", string.Empty);
cookie.Values.Add("user_password", string.Empty);

cookie.Expires.AddMinutes(2); //Cookie with empty credential strings.
}
Response.Cookies.Add(cookie); //Finally cookie is added to the http response.
}


Add following code-snips in Sign-out function...

if (Response.Cookies["Login_Credentials"] != null) {
HttpCookie cookie = Request.Cookies.Get("Login_Credentials");

cookie.Expires = DateTime.Now.AddDays(30);

Response.Cookies.Add(cookie);
}

Now after singing out, again cookie is added to the response. It will be tracked in login page at Page Load...

if(!IsPostPack)
{
//Remember me...
if (Request.Cookies["Login_Credentials"] != null)
{
HttpCookie cookie = Request.Cookies.Get("Login_Credentials");
txtUserName.Text = cookie.Values["user_name"];

//Notice that how the value of password is set.
txtPassword.Attributes.Add("value", cookie.Values["user_password"]);

if (txtUserName.Text != "")
chkRememberMe.Checked = true;
else
chkRememberMe.Checked = false;
}
}

This code is again a tested one.