Tuesday, February 11, 2014

Autonticatin page codes


Login Page


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using A = System.Web.Security;
using BL;

namespace PerfectWeb.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated) {

                Response.Redirect(A.FormsAuthentication.DefaultUrl);
            }

         
        }

        BL.UserEntity objEnt = new UserEntity();
        BL.User objUser = new User();

        protected void btnLogin_Click(object sender, EventArgs e)
        {
          
            if (Page.IsValid)
            {
                bool isOK = false;
                try
                {
                    objEnt.UserName = txtUserName.Text;
                    objEnt.Password = txtPassword.Text;
                    isOK = objUser.ValidateUser(objEnt);
                }
                catch (EntityEx ex)
                {

                    lblMessage.Text = ex.Message;
                    return;
                }
                catch(Exception ex) {

                    lblMessage.Text = ex.Message;
                    if (Trace.IsEnabled)
                    {
                        Trace.Warn(ex.Message);
                    }
                }

                if (isOK)
                {

                    Session["User"] = objEnt.UserName;
                    A.FormsAuthentication.SetAuthCookie(objEnt.UserName, ChkIsLog.Checked);
                   // Roles.AddUserToRole(objEnt.UserName, "Admin");
                    A.FormsAuthentication.RedirectFromLoginPage(objEnt.UserName, ChkIsLog.Checked);
                }
                else {

                    lblMessage.Text = "invalied";
                }

            }
           
        }

    }
}

////////////////////

Logout Page


   protected void HeadLoginStatus_LoggingOut(object sender, LoginCancelEventArgs e)
        {
            A.FormsAuthentication.SignOut();

            Response.Redirect(A.FormsAuthentication.LoginUrl);
        }


No comments:

Post a Comment

Sql server row level policy