示例#1
0
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        String Collectpassword = Utility.GetMd5Sum(tbPassword.Text);
        Simplicity.Data.SimplicityEntities DatabaseContext = new Simplicity.Data.SimplicityEntities();
        Simplicity.Data.User user = (from u in DatabaseContext.Users
                                     where u.Email == tbUserName.Text
                                     && u.Password == Collectpassword
                                     && u.Verified == true
                                     && u.Enabled == true
                                     select u).FirstOrDefault();

        if (user != null)
        {
            var userAuthorisedForThisProduct = user.UserProducts.Where(userProd => userProd.ProductID == int.Parse(AppSettings["HSProductIDInSimplicity"]));
            if (userAuthorisedForThisProduct.Count() <= 0)
            {
                errorPanel.Visible = true;
                SetErrorMessage("You are not authorized to use Health And Safety.");
            }

            else if (userAuthorisedForThisProduct.Count() > 0)
            {
                Simplicity.Data.Session session = new Simplicity.Data.Session();
                session.SessionUID = System.Guid.NewGuid().ToString();
                session.User = user;
                session.StartTime = DateTime.Now;
                session.LastActivityTime = DateTime.Now;
                session.EndTime = DateTime.Now.AddMinutes(30);
                session.IP = Request.UserHostAddress;
                DatabaseContext.AddToSessions(session);
                DatabaseContext.SaveChanges();
                FormsAuthentication.SetAuthCookie(session.SessionUID, false);
                Session[WebConstants.Session.USER_ID] = user.UserID;
                Session["userName"] = user.Email;
                Session["userFullName"] = user.FullName;
                //Session["isTrial"] = user.UserProducts.FirstOrDefault().IsTrial;
                Session["sessionID"] = session.SessionUID;

                log.Info("User successfully logged in.");

                if (Session[WebConstants.Session.RETURN_URL] != null)
                {
                    Response.Redirect((string)Session[WebConstants.Session.RETURN_URL]);
                }
                else if (Request["GOTO_URL"] != null)
                {
                    Response.Redirect((string)Request["GOTO_URL"]);
                }
                else
                {
                    Response.Redirect("TermsConditions.aspx", false);
                }
            }
        }
        else
        {
            errorPanel.Visible = true;
            SetErrorMessage(WebConstants.Messages.Error.CANNOT_LOGIN);
        }
    }
 /// <summary>
 /// Deprecated Method for adding a new object to the Sessions EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToSessions(Session session)
 {
     base.AddObject("Sessions", session);
 }
 /// <summary>
 /// Create a new Session object.
 /// </summary>
 /// <param name="sessionID">Initial value of the SessionID property.</param>
 /// <param name="sessionUID">Initial value of the SessionUID property.</param>
 /// <param name="userID">Initial value of the UserID property.</param>
 /// <param name="startTime">Initial value of the StartTime property.</param>
 /// <param name="lastActivityTime">Initial value of the LastActivityTime property.</param>
 /// <param name="endTime">Initial value of the EndTime property.</param>
 /// <param name="iP">Initial value of the IP property.</param>
 public static Session CreateSession(global::System.Int32 sessionID, global::System.String sessionUID, global::System.Int32 userID, global::System.DateTime startTime, global::System.DateTime lastActivityTime, global::System.DateTime endTime, global::System.String iP)
 {
     Session session = new Session();
     session.SessionID = sessionID;
     session.SessionUID = sessionUID;
     session.UserID = userID;
     session.StartTime = startTime;
     session.LastActivityTime = lastActivityTime;
     session.EndTime = endTime;
     session.IP = iP;
     return session;
 }