要使用SQL的PWDCOMPARE 函數,必須先將邏輯寫進StoreProcedure或是SQL function裡面,這裡以StoreProcedure為例
- 先建立StoreProcedure在裡面使用PWDCOMPARE函數比對密碼是否正確,成功傳回1,失敗傳回0
create procedure sp_CheckLoginPassword @Account varchar(50), @Password varchar(150) as declare @validate int set @validate = ( select PWDCOMPARE(@Password,tcuser.Password) from tcuser where tcuser.Account = @Account) select isnull(@validate,0)* 建立Entity Framwork,將剛剛寫的StoreProcedure更新進來
- 透過Entity Framwork建立出來的物件呼叫該方法就大功告成了!!
public bool CheckPassword()
{
//傳回0表示驗證失敗; 1表示成功
NTIEntities db = new NTIEntities();
bool isValidate = db.sp_CheckLoginPassword(this.Account, this.Password).First() == 1 ? true : false;
return isValidate;
} - 驗證方法成功後呼叫FormsAuthentication.RedirectFromLoginPage(),告訴網站該使用者已經成功登入 ```csharp
if (Acclogin.CheckPassword())
{ FormsAuthentication.RedirectFromLoginPage(Acclogin.Account, false);
return Redirect(returnUrl);
}