所以我試圖讓一個簡單的系統工作,其中我有一個asp.net mvc web應用程序,表單身份驗證已經啓動並且與創建的用戶一起運行。使用mvc控制器/視圖,我可以毫無問題地登錄。ASP.NET MVC + Silverlight +表單身份驗證
然後,我將Silverlight應用程序添加到解決方案中,使用現有的Web應用程序作爲主機。我創建了一個Silverlight啓用Web服務並添加以下代碼的經營合同:
[OperationContract]
public bool Authenticate(string username, string password)
{
if (FormsAuthentication.Authenticate(username, password))
{
FormsAuthentication.SetAuthCookie(username, false);
return true;
}
return false;
}
在Silverlight應用程序,我添加了兩個文本框和一個按鈕和一個服務參考WCF服務。在按鈕單擊事件,我有這樣的代碼:
void login_Click(object sender, RoutedEventArgs e)
{
AuthenticationService.AuthenticationClient client = new AuthenticationClient();
client.AuthenticateCompleted += new EventHandler<AuthenticateCompletedEventArgs>(client_AuthenticateCompleted);
client.AuthenticateAsync(username.Text, password.Text);
}
void client_AuthenticateCompleted(object sender, AuthenticateCompletedEventArgs e)
{
if (e.Result)
{
MessageBox.Show("Success");
}
else
{
MessageBox.Show("Error");
}
}
所以,問題是,當我輸入我的登錄信息,然後單擊按鈕,我得到的是錯誤框。我似乎無法得到它來驗證用戶。
我失去了什麼?
UPDATE: 以下是錯誤我在異步完整的處理程序得到:
行:86 錯誤:未處理的錯誤在Silverlight應用程序 代碼:4004
類別:ManagedRuntimeError
留言信息:System.NullReferenceException : 你調用的對象是空的。 在UserPortal.MainPage.client_AuthenticateCompleted(對象發件人,AuthenticateCompletedEventArgs E) 在UserPortal.AuthenticationService.AuthenticationClient.OnAuthenticateCompleted(對象狀態)
更新2: 所以我上面貼的錯誤是因爲e.Error屬性一片空白。所以我沒有從認證服務獲得任何特定的錯誤。有什麼我需要在web.config中更改以通過silverlight工作嗎?
<authentication mode="Forms">
<!-- forms loginUrl="~/Account/LogOn" timeout="2880"/ -->
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/"/>
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</roleManager>
你可以請嘗試MessageBox.Show(e.ToString())或MessageBox.Show(e.Error.ToString())或嘗試打印完整的錯誤,你會得到究竟是什麼錯誤。 – 2009-09-03 06:56:27