2014-11-27 3 views
0

로그인 프로세스를 처리하는 LINQ 문이 있습니다. 유효한 사용자 이름과 암호 조합이 전달되면 제대로 작동합니다. 그러나 유효하지 않은 자격 증명을 테스트 할 때 < < <으로 표시된 줄에 NullReferenceException 오류가 표시됩니다. -------------- 잘못된 자격 증명을 올바르게 처리하는 데 도움이 필요하십니까? 더 DBLogIn 기록 당신이주는 기준과 일치하지가 있다면Linq Select 문 도움말 필요 - FirstOrDefault 사용 후 NullReferenceException

public int _accountID; 
public int _securityLevelID; 
public void GetLoginInfo(string EmailAddress, string Password) 
{ 
    LoginItem l = null; 

    { 
     try 
     { 
      using (RootsDataContext RDC = new RootsDataContext()) 

       l = (from a in RDC.DBLogIns 
        where a.EmailAddress == EmailAddress 
        && a.Password == Password 
        && a.IsActive == 1 

        select new LoginItem 
        { 
         AccountIDFK = a.AccountIDFK, 
         SecurityLevelIDFK = a.SecurtityLevelIDFK, 

        }).FirstOrDefault(); 

      _accountID = (int)l.AccountIDFK;  <<<---------------- 
      _securityLevelID = (int)l.SecurityLevelIDFK; 

      if (_accountID < 1 || _accountID == null) 
      { 
       lbl_LoginStatus.Text = "Invalid"; 
      } 

     } 


     catch (Exception ex) 
     { 
      string error = ex.Message; 
     } 


     if (_accountID > 0) 
     { 
      if (_accountID == 1 && _securityLevelID == 1) // [Quentin] 
      { 
       Response.Redirect("~/AccountsMaster.aspx"); 
      } 

      if (_accountID > 1 && _securityLevelID == 2) // [Companies]  
      { 
       Response.Redirect("~/CompanyMaster.aspx"); 
      } 

      if (_accountID > 1 && _securityLevelID == 3) // [Branch] 
      { 
       Response.Redirect("~/BranchMaster.Aspx"); 
      } 

      if (_accountID > 1 && _securityLevelID == 4) // [Clients] 
      { 
       Response.Redirect("~/Home.aspx"); 
      } 
     } 
    } 
}  

답변

2

일치하는 항목이 발견되면 DBLogIn 개체이거나 일치하지 않으면 null 개체입니다.

  • 당신은 데이터베이스에 store passwords directly 안 :

    // ... }).FirstOrDefault(); 
    if (l != null) 
    { 
        _accountID = (int)l.AccountIDFK; 
        _securityLevelID = (int)l.SecurityLevelIDFK; 
    } 
    

    몇 가지 다른 점을 고려 :

    당신은 재산 AccountIDFKSecurityLevelIDFK에 접근하기 전에 null을 확인해야합니다. 보다 안전한 방법은 데이터베이스에 해시 된 (그리고 잠재적으로 소금에 절인) 암호를 저장 한 다음 사용자 (EmailAddress and Active = 1)를 찾은 다음 사용자가 입력 한 내용과 DB에 저장된 내용을 비교하는 것입니다. -

  • 이 코드 swallows exceptions이 문제를 악몽 진단한다 :

    catch (Exception ex) 
    { 
        string error = ex.Message; 
    } 
    
  • Don't make fields public (public int _accountID;를) - 그들이 외부에서 사용하지 않는 경우이를 비공개로하거나 외부에서 볼 수있는 경우 (자동 생성) 속성으로 변환 수업에서.
1

FirstOrDefault 메소드는 null를 돌려 것입니다, 그래서 당신은 l(int)l.AccountIDFK에 액세스하기 전에 먼저 null의 경우 확인해야합니다. 또한, l가 null했을 때의 lbl_LoginStatus.Text = "Invalid";처럼 보이는, 그래서 다음과 같이 if (_accountID < 1 || _accountID == null) 블록을 제거하고 코드를 변경해야합니다

또는
if (l != null) 
{ 
    _accountID = (int)l.AccountIDFK; 
    _securityLevelID = (int)l.SecurityLevelIDFK; 
} 
else 
{ 
    // logic when l is null 
    lbl_LoginStatus.Text = "Invalid"; 
} 

l가 null의 경우 당신은 또한 확인 C# Ternary Operator을 사용할 수 있습니다

당신이 중 하나를 얻을 것이다

// ... 
    }).FirstOrDefault(); 

말을함으로써

_accountID = l != null ? (int)l.AccountIDFK : 0; 
_securityLevelID = l != null ? (int)l.SecurityLevelIDFK : 0; 

if (_accountID < 1) 
{ 
    lbl_LoginStatus.Text = "Invalid"; 
} 
0

'l'에 null 값이 있는지 확인한 후에 사용해야합니다.

if(l!=null) 
{ 
_accountID = (int)l.AccountIDFK;  
      _securityLevelID = (int)l.SecurityLevelIDFK; 
} 
else 
{ 
lbl_LoginStatus.Text = "Invalid"; 
} 
0

당신은 당신의 코드에서 null을 얻을 경우 query.So 일치하는 목록에 항목이없는 경우 Linq에 FirstOrDefault는 null를 돌려 사용자 로그인이 유효하지 않음을 의미한다. 당신이 원하는 무엇이든 할

public int _accountID; 
 
    public int _securityLevelID; 
 
    public void GetLoginInfo(string EmailAddress, string Password) 
 
    { 
 
     LoginItem l = null; 
 

 
     { 
 
      try 
 
      { 
 
       using (RootsDataContext RDC = new RootsDataContext()) 
 

 
        l = (from a in RDC.DBLogIns 
 
         where a.EmailAddress == EmailAddress 
 
         && a.Password == Password 
 
         && a.IsActive == 1 
 

 
         select new LoginItem 
 
         { 
 
          AccountIDFK = a.AccountIDFK, 
 
          SecurityLevelIDFK = a.SecurtityLevelIDFK, 
 

 
         }).FirstOrDefault(); 
 
       if(l==null || _accountID < 1 || _accountID == null) 
 
       { 
 
       lbl_LoginStatus.Text = "Invalid"; 
 
       Response.Redirect("~/InvalidCredentials.aspx"); // redirect to invalid login page. 
 
       } 
 
       else 
 
       { 
 
       _accountID = (int)l.AccountIDFK; 
 
       _securityLevelID = (int)l.SecurityLevelIDFK; 
 

 
       } 
 
      } 
 

 

 
      catch (Exception ex) 
 
      { 
 
       string error = ex.Message; 
 
      } 
 

 

 
      if (_accountID > 0) 
 
      { 
 
       if (_accountID == 1 && _securityLevelID == 1) // [Quentin] 
 
       { 
 
        Response.Redirect("~/AccountsMaster.aspx"); 
 
       } 
 

 
       if (_accountID > 1 && _securityLevelID == 2) // [Companies]  
 
       { 
 
        Response.Redirect("~/CompanyMaster.aspx"); 
 
       } 
 

 
       if (_accountID > 1 && _securityLevelID == 3) // [Branch] 
 
       { 
 
        Response.Redirect("~/BranchMaster.Aspx"); 
 
       } 
 

 
       if (_accountID > 1 && _securityLevelID == 4) // [Clients] 
 
       { 
 
        Response.Redirect("~/Home.aspx"); 
 
       } 
 

 
      } 
 

 

 
     } 
 

 
    }

0

당신은 LoginItem의 기본 값 null에 대한 검사를해야한다, 그 경우는 null (잘못된 자격 증명의 경우).