Wednesday, January 26, 2005

complex javascript confirm

normally if you want to warn user before he click some button to delete or update something, you can use
Button.Attributes.Add("onclick","return confirm('Are you sure?')");
However, the javascript confirm function is very simple except you can modify some text of it. In some case, you need to perform some additional checking before you pop up the confirm dialog box. For example, in this web application, it only requires the confirm window pop up when the domain the user entered is equal to the user's domain, so you need some custumorized JS function to perform this checking and confirmation.

addWhite.Attributes.Add("onclick","return SameAddress_Confirm();");

where SameAddress_Confirm() is the JS function I wrote to do this:

function SameAddress_Confirm() {
var mydomain = document.all["hJS"].value;
var addWhiteEntry = document.all["addWhiteEntry"].value;
var addBlackEntry = document.all["addBlackEntry"].value;
addWhiteEntry = TrimString(addWhiteEntry).split('@')[1];
addBlackEntry = TrimString(addBlackEntry).split('@')[1];
var ifAdd;
if((mydomain == addBlackEntry)(mydomain==addWhiteEntry))
{ //show confirm message
ifAdd = !confirm("Adding your own email address or domain to this list can have negative results and actually increase the amount of unwanted messages (spam) your inbox receives significantly.\n\nPlease see the help section topic 'Managing your Allowed Senders lists' for specific details\n\nThis entry can still be added by clicking 'cancel' below.");
}
else
ifAdd = true;
return ifAdd;
}

Monday, January 24, 2005

Check if Cookies are enabled in one page

Put these javascript code in the header of the login.aspx page to dected if cookies are enalbed

/***************************************************************************************

Thursday, January 06, 2005

Switching Between HTTP and HTTPS Automatically

a very decent way of implementing http and https switch using web.config

http://www.codeproject.com/aspnet/WebPageSecurity.asp

Monday, January 03, 2005

form authentication

form authentication, 3 requirements

i. If user click “save password”, the password will always be saved in the cookie unless he click “logout”.
ii. Otherwise, user need to input username and password when login.
iii. If the user just close the browser, he should be log out from the system, and need to use password to login

implementation:


FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1,
user.GID.ToString(), DateTime.Now,DateTime.Now.AddMinutes(1),
chkRemember.Checked, roles);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

if(chkRemember.Checked)
authCookie.Expires = DateTime.Now + (new TimeSpan(14,0,0,0));

Response.Cookies.Add(authCookie);


/********************************/
only if chkRemember.Checked, then set the logon cookie 2 weeks of validation.