Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使登录会话更快?_Javascript_C#_Entity Framework_Linq_Login - Fatal编程技术网

Javascript 如何使登录会话更快?

Javascript 如何使登录会话更快?,javascript,c#,entity-framework,linq,login,Javascript,C#,Entity Framework,Linq,Login,我有一个工作登录页面,我使用实体框架和JS+jQuery(用于验证)编写了该页面。问题是代码需要很长的加载时间(最多45秒),我认为这是因为脚本,我的问题是,有没有办法让代码看起来更简单,这样我就可以提高性能 下面是一个快照: 基本上,脚本包含提示用户字段是否为空或其中一个字段为空(用户名或密码)的代码。我知道问题不在我的电脑上,因为除了这个项目,其他项目运行得更快 using System; using System.Collections.Generic; using System.Linq

我有一个工作登录页面,我使用实体框架和JS+jQuery(用于验证)编写了该页面。问题是代码需要很长的加载时间(最多45秒),我认为这是因为脚本,我的问题是,有没有办法让代码看起来更简单,这样我就可以提高性能

下面是一个快照:

基本上,脚本包含提示用户字段是否为空或其中一个字段为空(用户名或密码)的代码。我知道问题不在我的电脑上,因为除了这个项目,其他项目运行得更快

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

using System.Web.UI.WebControls;

namespace BloodBankSystem
{
    public partial class LogIn_Page : System.Web.UI.Page
    {
        public string Username;
      //  public int role { get; set; }
       // public BloodBankDataContext context = new BloodBankDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
          //  LblError.Visible = false;
        }

        public static string CreatePasswordHash(string plain_password)
        {
            byte[] data = System.Text.Encoding.ASCII.GetBytes(plain_password);
            data = new System.Security.Cryptography.HMACSHA256().ComputeHash(data);

            return System.Text.Encoding.ASCII.GetString(data);
        }

    protected void ShowMessage(string Message, MessageType Type)
    {
      ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), "ShowMessage('" + Message + "','" + Type + "');", true);
      ShowMessage("Invalid username and password combination.", MessageType.Error);
    }

    public enum MessageType { Success, Error, Warning };


    public static Tuple<int,bool> IsValidLogIn(string username, string password)
    {
      bool valid = false;
      int role ;
      using (BloodBankSystemDataContext context = new BloodBankSystemDataContext())
      {
        Donor user = (from _user in context.Donors
                      where _user.DonorKey == username &&
                     _user.Password == password
                      select _user).FirstOrDefault();

        Staff admin = (from _admin in context.Staffs
                       where _admin.StaffKey == username &&
                       _admin.Password == password
                       select _admin).FirstOrDefault();


        password = CreatePasswordHash(password);

        if (user != null)
        {
          role = 2;
          valid = true;        
        }
        else if (admin != null)
        {
          role = 1;         
          valid = true;         
        }
        else
        {
          role = 0;
          valid = false;          
        }


        return  new Tuple<int, bool>(role, valid);
        // tuple;
      }
      }


        public void LogIN()
        {
            string strPassword = Page.Request.Form["TxtPassword"].ToString();
            string strUsername = Page.Request.Form["TxtUsername"].ToString();
            bool valid = IsValidLogIn(strUsername, strPassword).Item2;
            int role = IsValidLogIn(strUsername, strPassword).Item1;

      // string strLogIn = Page.Request.Form["btnLogIn"].ToString();

      if (!(strUsername.ToString().Equals("")) || !(strPassword.ToString().Equals("")))
      {
        if (valid)
        {
          if (role == 1)
          {
            Response.Redirect("StaffHomepage_Page.aspx");
          }

          if (role == 2)
          {
            Response.Redirect("Home_Page.aspx");

          }
        }
        else if (IsValidLogIn(strUsername, strPassword).Item2 == false && IsValidLogIn(strUsername, strPassword).Item1 == 0)
        {
          ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + "Invalid username and password combination!" + "');", true);
         // ShowMessage("Invalid username and password combination.", MessageType.Error);
          LblError.Visible = true;

        }
      }
      else
      {
        ScriptManager.RegisterStartupScript(this, GetType(), "script", "DoValidate();", true);
        ShowMessage("Invalid username and password combination.", MessageType.Error);
      }
        }
        public void LogIn_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "script", "DoValidate();", true);
          // LblError.Visible = true;
            LogIN();
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
名称空间血库系统
{
公共部分类登录页面:System.Web.UI.Page
{
公共字符串用户名;
//公共int角色{get;set;}
//public BloodBankDataContext=new BloodBankDataContext();
受保护的无效页面加载(对象发送方、事件参数e)
{
//LblError.Visible=false;
}
公共静态字符串CreatePasswordHash(字符串普通\u密码)
{
字节[]数据=System.Text.Encoding.ASCII.GetBytes(普通密码);
data=新系统.Security.Cryptography.HMACSHA256().ComputeHash(数据);
返回System.Text.Encoding.ASCII.GetString(数据);
}
受保护的void ShowMessage(字符串消息、消息类型)
{
ScriptManager.RegisterStartupScript(this,this.GetType(),System.Guid.NewGuid().ToString(),“ShowMessage(“+Message+”,“+Type+”);”,true);
ShowMessage(“无效的用户名和密码组合。”,MessageType.Error);
}
公共枚举消息类型{成功,错误,警告};
公共静态元组IsValidLogIn(字符串用户名、字符串密码)
{
bool valid=false;
智力角色;
使用(BloodBankSystemDataContext=new BloodBankSystemDataContext())
{
捐赠者用户=(来自上下文中的_用户)。捐赠者
其中_user.DonorKey==用户名&&
_user.Password==密码
选择_user).FirstOrDefault();
Staff admin=(来自context.Staff中的_admin
其中_admin.StaffKey==用户名&&
_admin.Password==密码
选择_admin).FirstOrDefault();
password=CreatePasswordHash(密码);
如果(用户!=null)
{
角色=2;
有效=真;
}
else if(admin!=null)
{
角色=1;
有效=真;
}
其他的
{
角色=0;
有效=错误;
}
返回新元组(角色,有效);
//元组;
}
}
公共无效登录()
{
字符串strPassword=Page.Request.Form[“TxtPassword”].ToString();
字符串strUsername=Page.Request.Form[“TxtUsername”].ToString();
bool valid=IsValidLogIn(strUsername,strPassword);
int role=IsValidLogIn(strUsername,strPassword);
//字符串strLogIn=Page.Request.Form[“btnLogIn”].ToString();
if(!(strUsername.ToString().Equals(“”)| |!(strPassword.ToString().Equals(“”))
{
如果(有效)
{
如果(角色==1)
{
重定向(“StaffHomepage_Page.aspx”);
}
如果(角色==2)
{
重定向(“Home_Page.aspx”);
}
}
else if(IsValidLogIn(strUsername,strPassword).Item2==false&&IsValidLogIn(strUsername,strPassword).Item1==0)
{
ClientScript.RegisterStartupScript(this.GetType(),“myalert”,“alert”(“+”无效的用户名和密码组合!”+“);”,true);
//ShowMessage(“无效的用户名和密码组合。”,MessageType.Error);
LblError.Visible=真;
}
}
其他的
{
RegisterStartupScript(this,GetType(),“script”,“DoValidate();”,true);
ShowMessage(“无效的用户名和密码组合。”,MessageType.Error);
}
}
公共无效登录\u单击(对象发送者,事件参数e)
{
RegisterStartupScript(this,GetType(),“script”,“DoValidate();”,true);
//LblError.Visible=真;
登录();
}
}
}
一些问题#1您的密码哈希是在您进行查找之后生成的吗?这意味着密码是纯文本存储的

但是对于性能问题:断点和数据库探查器可以告诉您很多关于性能的信息。在以下位置设置断点:
使用(BloodBankSystemDataContext=new BloodBankSystemDataContext())

启动DbContext需要多长时间?大型、复杂的上下文可能需要一段时间才能启动。在这种情况下,采用更多的有界上下文方法(较小的上下文用于服务特定领域,如身份验证)可以提高性能

接下来,您将构建上下文并在每次登录时调用数据库两次:

bool valid = IsValidLogIn(strUsername, strPassword).Item2;
int role = IsValidLogIn(strUsername, strPassword).Item1;
对于元组结果,应该是:

var tuple = IsValidLogIn(strUserName, strPassword);
bool valid = tuple.Item2;
int role = tuple.Item1;
虽然我不建议使用元组,但只需创建一个私有容器来返回所需的相关详细信息,以及角色的枚举,而不是任意整数值。这使得它更具可读性。更新登录验证方法以返回此结果容器的实例,而不是元组。例如:

private enum UserRoles
{
   None = 0,
   User,
   Admin
}

private struct LoginResult
{
   bool IsValid = false;
   UserRole Role = UserRoles.None;
}
然后

尽量避免使用大量的条件代码(ifs、else,都是嵌套的),这会使代码复杂、难以理解,并为出现错误的情况留下很大的空间。意外的条件组合会导致计划外的执行路径

接下来,在实体Linq查找中使用
.Any()

bool isDonor = context.Donors
                 .Any(d => d.DonorKey == username 
                        && d.Password == password);

bool isAdmin = context.Staffs
                 .Any(s => s.StaffKey == username 
                        && s.Password == password);
SQL将尽可能地执行该操作
bool isDonor = context.Donors
                 .Any(d => d.DonorKey == username 
                        && d.Password == password);

bool isAdmin = context.Staffs
                 .Any(s => s.StaffKey == username 
                        && s.Password == password);