[英]ASP.NET MVC + Silverlight + Forms Authentication
所以我試圖讓一個簡單的系統工作,在這里我有一個具有表單身份驗證的asp.net mvc Web應用程序,並已與創建的用戶一起運行。 我可以使用MVC控制器/視圖正常登錄。
然后,我使用現有的Web應用程序作為主機,將Silverlight應用程序添加到解決方案中。 我創建了啟用了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");
}
}
所以問題是,當我輸入登錄信息並單擊按鈕時,我得到的只是錯誤框。 我似乎無法獲得它來驗證用戶身份。
我想念什么?
更新:這是我在異步完成處理程序中得到的錯誤:
行:86錯誤:Silverlight應用程序代碼中未處理的錯誤:4004
類別:ManagedRuntimeError
消息:System.NullReferenceException:對象引用未設置為對象的實例。 在UserPortal.AuthenticationService.AuthenticationClient.OnAuthenticateCompleted(對象狀態)在UserPortal.MainPage.client_AuthenticateCompleted(對象發送者,AuthenticateCompletedEventArgs e)
更新2:所以我上面發布的錯誤是因為e.Error屬性為null。 所以我沒有從身份驗證服務中收到任何特定的錯誤。 我需要在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>
使用WCF並在開發服務器上運行時,需要安裝適當的證書。 它不是Silverlight它的WCF客戶端代理,它試圖驗證您的證書並失敗,我認為。 當您嘗試從ASP或瀏覽器中找到它時會發生什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.