2013-10-23 250 views
-1

當我的數據庫中不存在用戶時,登錄功能出現錯誤。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.

+0

你可能需要一個if else語句。如果用戶存在,繼續,否則顯示一個jQuery彈出窗口或將標籤文本更改爲「Login Unsuccessful」或類似的東西。 – Humpy

+0

@Humpy,是的,我有一個條件就是返回的布爾,即使我到達else塊也會出現錯誤 – meda

回答

0

我通過用我自己的連接字符串替換所有ApplicationServices連接字符串來解決這個問題。

然後,我跑到下面的命令:

aspnet_regsql.exe -S DBServerName -U DBLogin -P DBPassword -A all -d DBName 
0

我有這種事發生在我和它取決於它是在什麼頁面。這是因爲除非初始化簡單的成員資格,否則認證/授權機制將失敗並給出奇怪的(不真實的!)消息。

在我的情況下,將屬性[InitializeSimpleMembership]添加到Controller類將確保連接已正確設置並擺脫了錯誤。

+0

它不是MVC,我該如何使用這個屬性? – meda

+0

哦,對不起。這似乎是我必須從網上獲得的我自己的課程。代碼在這裏http://pastebin.com/t7k8dZjz – Lukos

+0

它的ASP.NET應用程序不是MVC的一個 – meda