當我的數據庫中不存在用戶時,登錄功能出現錯誤。ASP.NET身份驗證問題
這裏是我的功能:
private bool AdminIsValid(string username, string password)
{
using (var db = new AdminEntities())
{
return db.users.Any(u => u.password.Trim() == password &&
u.uname.Trim() == username);
}
}
它按預期工作BU當用戶不存在,我碰到下面的錯誤。
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified).
我懷疑ApplicationServices
連接字符串是這個原因,你知道,自帶的模板之一。
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
我還沒有找到一種方法來擺脫它,當我刪除它,我得到不同的錯誤。 當用戶登錄有效時,則不會引發錯誤。
當密碼是錯誤的,我得到:
Failed to generate a user instance of SQL Server due to a failure in starting the process for the user instance. The connection will be closed.
你可能需要一個if else語句。如果用戶存在,繼續,否則顯示一個jQuery彈出窗口或將標籤文本更改爲「Login Unsuccessful」或類似的東西。 – Humpy
@Humpy,是的,我有一個條件就是返回的布爾,即使我到達else塊也會出現錯誤 – meda