Sunday, 22 July 2012

Login without Form Authentication

public class Authentication : System.Web.UI.Page
    {
        /// <summary>
        ///
        /// </summary>
        /// <param name="e"></param>
        #region "Event"
        protected override void OnInit(EventArgs e)
        {
            try
            {
                if (HttpContext.Current.Session.IsNewSession)
                {
                    string szCookieHeader = HttpContext.Current.Request.Headers["Cookie"];
                    if ((null != szCookieHeader) && (szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
                    {
                        HttpContext.Current.Response.Redirect("~/Login.aspx?session=expired", false);
                        return;
                    }
                }
                if (HttpContext.Current.Session["UserSession"] == null)
                {
                    HttpContext.Current.Response.Redirect("~/Login.aspx", false);
                    return;
                }


            }
            catch (Exception ex)
            {
                LogException.HandleException(ex);
            }
        }
        #endregion
 #region Globalization
        protected string LanguageId = string.Empty;
        protected override void InitializeCulture()
        {
            if (Session["LanguageId"] != null)
            {
                LanguageId = Convert.ToString(Session["LanguageId"]);
                UICulture = LanguageId;
            }
            base.InitializeCulture();
        }
        #endregion
    }
---------------------------------------------------------------------------------------------------------------
public class UserSession
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string CityId { get; set; }
        public string Mobile { get; set; }
        public string Email { get; set; }              
    }
----------------------------------------------------------------------------------------------------------
Login.aspx.cs
 private void Login()
        {
            string msgBox = string.Empty;
            try
            {
                DataLogin objDataLogin = new DataLogin();
                DataTable dtresult = objDataLogin.UserAuthentication(txtName.Text, txtPwd.Text);
                if (dtresult == null || dtresult.Rows.Count == 0)
                    return;
                switch (Convert.ToInt32(dtresult.Rows[0]["Result"].ToString()))
                {
                    case 1:
                        UserSession objUserSession = new UserSession();
                        objUserSession.Id = dtresult.Rows[0]["id"].ToString();
                        objUserSession.CityId = dtresult.Rows[0]["cityid"].ToString();
                        Session["UserSession"] = objUserSession;
                        Response.Redirect("Account.aspx", false);
                        break;
                    }

            }
            catch (Exception ex)
            {
                LogException.HandleException(ex);
            }
        }
-------------------------------------------------------------------------------------------------------------
public partial class Account: Authentication
    {
   protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    if (Session["userSession"] == null)
                        return;
                   }
                catch (Exception ex)
                {
                    LogException.HandleException(ex);
                }

            }
        }
}

No comments:

Post a Comment