Tuesday, April 29, 2008

Securing website without using default DB but using FormsAuthentication

Ok, Im so excited to have finally figured this out. Many times a programmer is going to want to give access to certain folders in their site but not want to use the default database that comes with VS.

so here's the code

//check if they login to database or xml file, the 1st 2 lines of code will be you connecting to
//DB and then determining what that users Status should be.

Login Login1 = (Login)LoginView1.FindControl("Login1");
if ((Login1.UserName == "test") && (Login1.Password == "pass"))
{

if (Request.QueryString["ReturnUrl"] != null)
{
FormsAuthentication.RedirectFromLoginPage("admin", false);
//if my DB says they are admin then i make them an admin and redirect to page they where
//trying to access
}
else
{
//if my DB says they are admin then i make them an admin and redirect to default.aspx
FormsAuthentication.SetAuthCookie("admin", false);
Response.Redirect("default.aspx");
}
}
else
{
//login from DB or XML failed
}

heres the web.config file placed in folder u want to protect i had to take out tags cause blog doesn't like them. deal with it

system.web
authorization
allow users="admin"/
deny users="*" /
/authorization
/system.web

Monday, April 28, 2008

using roles and logins

protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
{
Roles.AddUserToRole(CreateUserWizard1.UserName, "client");
}



protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
Login Login1 = (Login)LoginView1.FindControl("Login1");
if (Membership.ValidateUser(Login1.UserName, Login1.Password))
{
if (Request.QueryString["ReturnUrl"] != null)
{
Response.Write("redirect");
FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);
}
else
{
FormsAuthentication.SetAuthCookie(Login1.UserName, false);
Response.Write("set cookie");
e.Authenticated = true;
}
}
else
{
Response.Write("you're not welcomed");
}
}








Tuesday, April 22, 2008

Grab asp, asp.net Sessions from javascript

window.onload = function() {
var a = 'Session["LoginStatus"]';
alert(a); }


place this in header surrounded by javascript tags sorry can't place them in here without it becoming code, also surround the Session["LoginStatus"] with tags you use to signify asp/ asp.net code, i bet this will work with php too though haven't tested.

Wednesday, April 16, 2008

Create Wallpaper/Background for folder in XP

2 Steps

step 1
[{BE098140-A513-11D0-A3A4-00C04FD706EC}]
iconarea_image=C:\My Pictures\picture.jpg

place above code in Desktop.ini doc in folder you want to have background, There doesn't need to be any other text in this file. (this doc won't show cause its hidden by default view)

step 2
Go to command line and type this
attrib +s C:\My Pictures

Thats it.

Monday, April 14, 2008

click on button and move an image in javascript

//**********I'm also moving a div with a background image to make it appear to to scroll upward**********************************

ypos =375;
ry = 375;
height = 10;
var timer;


function movescroll(){
if(ry > 10)
{
ry= ry-10;
height = height + 10 //grow div1
obj=document.getElementById("top");
obj.style.top=ry +"px";
obj=document.getElementById("div1");
obj.style.height= height +"px";
obj.style.top= (ry + 3)+"px";
}
else
{
clearInterval(timer);
}
}
function fire(){
height = 10; // reset
ry= ypos; // reset
obj=document.getElementById("top");
obj.style.top= ry +"px";
timer=setInterval("movescroll()",5);
}

//***********add this code in img tag*************

onclick="clearInterval(timer);fire()"

Monday, April 07, 2008

Wow Nice Blog Name

I have know idea how i ended up with this blog name. I know i didn't create it, but since i just happen to know a little asp, I shant let this blog title go to waste.

Today I learned how to dynamically create a menu control reading from the sitemap.config file.
I spent a couple days on it and i think this is the best way to do it.

Dynamically Create Menu Control based on web.sitemap
//*************************************************************************
SiteMapNodeCollection baseCollection = new SiteMapNodeCollection(SiteMap.RootNode);
SiteMapNodeCollection parentCollection = SiteMap.RootNode.ChildNodes;
Menu menu2 = new Menu();
if (Session["User"] != null)
{
LocalUser user = (LocalUser)Session["User"];
int count = 1;
foreach (SiteMapNode parentNode in parentCollection)
{
if (user.AccessLevel >= int.Parse(parentNode.Description))
{
menu2.MenuItemClick += new MenuEventHandler(Menu_MenuItemClick);
MenuItem parentItem = new MenuItem("", "", "~/images/top_" + count.ToString() + "t.gif");
menu2.Items.Add(parentItem);
SiteMapNodeCollection childCollection = parentNode.ChildNodes;
foreach (SiteMapNode childNode in childCollection)
{
menu2.MenuItemClick += new
MenuEventHandler (Menu_MenuItemClick);//reader.MoveToAttribute(i);
MenuItem childItem = new MenuItem(childNode.Title, "", "", childNode.Url);
parentItem.ChildItems.Add(childItem);
}
count++;
}
}
}
//************don't forget to create the event*******************************

protected void Menu_MenuItemClick(object sender, MenuEventArgs e)
{ }
//***************notes****************************************************
I'm building the menu based on the user access level, i placed in the web.sitemap attribute description the level i wanted the parent to have.