首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> ASP.NET 基礎

Silverlight 呼叫 ASP.NET WCF 服務操作 ASPNETDB 會員資料庫

     編輯:ASP.NET 基礎

架構圖

Silverlight Client → WCF Service (ASP.NET 相容模式) → ASP.NET Membership API → ASPNETDB

伺服器端:建立 Silverlight 啟用的 WCF 服務

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MembershipService
{
    [OperationContract]
    public void CreateUser(string userName, string password, string email, 
                           string question, string answer)
    {
        MembershipCreateStatus status;
        Membership.CreateUser(userName, password, email, question, answer, true, out status);
        if (status != MembershipCreateStatus.Success)
            throw new FaultException("建立失敗: " + status);
    }
}

Web.config 關鍵設定

<system.serviceModel>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  <behaviors>
    <serviceBehaviors>
      <behavior>
        <serviceMetadata httpGetEnabled="true" />
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <bindings>
    <basicHttpBinding>
      <binding name="secureBinding">
        <security mode="TransportCredentialOnly">
          <transport clientCredentialType="None" />
        </security>
      </binding>
    </basicHttpBinding>
  </bindings>
</system.serviceModel>

Silverlight 客戶端呼叫

var client = new MembershipServiceClient();
client.CreateUserCompleted += (s, e) =>
{
    if (e.Error != null) MessageBox.Show(e.Error.Message);
    else MessageBox.Show("註冊成功");
};
client.CreateUserAsync("user01", "pwd123", "a@b.com", "question", "answer");

注意事項

  • WCF 服務需啟用 `AspNetCompatibility` 才能存取 `HttpContext` / `Membership`。
  • Silverlight 受限於跨域政策,需在網站根目錄放置 `clientaccesspolicy.xml` 或 `crossdomain.xml`。
  • 生產環境建議改用 HTTPS + 自訂驗證 Token,避免明文傳輸密碼。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved