2013-02-04 37 views
1

我有基於SimpleMemebershipProvider的用戶身份驗證的ASP.NET MVC 4應用程序。使用Ms SQL數據庫。一切正常。我可以註冊新用戶,登錄,註銷等WCF和SimpleMembershipProvider。如何管理用戶?

問題是,我想創建Windows窗體應用程序可以連接到服務器,並通過憑據後,它可以驗證,如果用戶存在數據庫(註冊通過MVC),如果是這樣,做一些東西,例如更改用戶名或密碼。我的想法是使用WCF服務庫。我知道WCF的基本概念,或者至少我希望如此:)我知道有可能對用戶進行身份驗證。

我在網上搜索,但我沒有找到如何與simplememebership提供商做到這一點。我也試圖自己編寫WCF服務庫,並且我在下面創建了類似的東西,但它不起作用。當我測試並輸入錯誤的證書時,它會返回字符串「bad credentials」,這很好。但是,當我輸入有效的憑據時,它顯示一個錯誤「NullReference異常」在線:

if (WebSecurity.Login(UserName, password, persistCookie: false))

我不認爲它是安全之一:/

有人可以解釋我如何將這個或我在做什麼錯?或者,也許有比WCF更好的解決方案?

Sevice1.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.Serialization; 
using System.ServiceModel; 
using System.Text; 
using WebMatrix.WebData; 
using System.Web.Mvc; 


namespace AR_WCF_Library 
{ 
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together. 
    public class Service1 : IService1 
    { 
     public string GetData(string UserName, string password) 
     { 

      WebSecurity.InitializeDatabaseConnection("MyDB", "UserProfile", "UserId", "UserName", autoCreateTables: false); 

      if (WebSecurity.Login(UserName, password, persistCookie: false)) 
      { 
       return string.Format("Hello: {0}", UserName); 
      } 
      else 
      { 
       return "bad credentials"; 
      } 
     } 

    } 
} 

的App.config

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 

    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <connectionStrings> 

    <add name="MyDB" connectionString="Data Source=SERWER\MORPHEUS;Initial Catalog=AOR;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False" providerName="System.Data.SqlClient" /> 

    </connectionStrings> 
    <system.web> 
    <compilation debug="true" /> 

    <roleManager enabled="true" defaultProvider="SimpleRoleProvider"> 
     <providers> 
     <clear/> 
     <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/> 
     </providers> 
    </roleManager> 

    <membership defaultProvider="SimpleMembershipProvider"> 
     <providers> 
     <clear/> 
     <add name="SimpleMembershipProvider" 
      type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" 
      enablePasswordReset="true" /> 
     </providers> 
    </membership> 

    </system.web> 
    <!-- When deploying the service library project, the content of the config file must be added to the host's 
    app.config file. System.Configuration does not support config files for libraries. --> 
    <system.serviceModel> 
    <services> 
     <service name="AR_WCF_Library.Service1"> 
     <host> 
      <baseAddresses> 
      <add baseAddress = "http://localhost:8733/Design_Time_Addresses/AR_WCF_Library/Service1/" /> 
      </baseAddresses> 
     </host> 
     <!-- Service Endpoints --> 
     <!-- Unless fully qualified, address is relative to base address supplied above --> 
     <endpoint address="" binding="wsHttpBinding" contract="AR_WCF_Library.IService1"> 
      <!-- 
       Upon deployment, the following identity element should be removed or replaced to reflect the 
       identity under which the deployed service runs. If removed, WCF will infer an appropriate identity 
       automatically. 
      --> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
     <!-- Metadata Endpoints --> 
     <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
     <!-- This endpoint does not use a secure binding and should be secured or removed before deployment --> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <!-- To avoid disclosing metadata information, 
      set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/> 
      <!-- To receive exception details in faults for debugging purposes, 
      set the value below to true. Set to false before deployment 
      to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="False" /> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 

</configuration> 
+1

請看到類似問題的答案: http://stackoverflow.com/questions/13161841/using-webmatrix-webdata-websecurity-in-a-wpf-application – Jomit

回答

1

以下內容添加到任何的承載您的WCF服務的Web.config:

<system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 
</system.serviceModel>