0

我在ASP.NET MVC4應用程序中實現了一個簡單的成員。但是現在我需要實現創建用戶並驗證其中的登錄。爲什麼在WindowsForms中使用SimpleMembership時HttpContext爲null?

所以我開始複製我最近項目的一些代碼片段。作爲例子,這是在崩潰

  WebSecurity.InitializeDatabaseConnection("SimpleMembershipConnection", 
    "UserProfile", "UserId", "UserName", autoCreateTables: true); 
      bool val = WebSecurity.Login("1010", "pass"); // here throws the exception 

誤差表示,部分:

對象引用不設置爲一個對象的一個​​實例。參數名稱: httpContext。

而且,我只是進口一些庫,WebMatrix.WebData,WebMatrix.Data和的System.Web

這是我整個的app.config

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
    <configSections> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <connectionStrings> 
    ... 
    </connectionStrings> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> 
    </entityFramework> 
<roleManager enabled="true" defaultProvider="simple"> 
    <providers> 
    <clear /> 
    <add name="simple" type="WebMatrix.WebData.SimpleRoleProvider,    WebMatrix.WebData" /> 
    </providers> 
</roleManager> 
<membership defaultProvider="simple"> 
    <providers> 
    <clear /> 
    <add name="simple" type="WebMatrix.WebData.SimpleMembershipProvider,    WebMatrix.WebData" /> 
    </providers> 
</membership> 

如何我可以修復這個異常,關於在HttpContext中沒有得到null嗎?

+0

只是要清楚,你的問題陳述Windows窗體;你是否試圖在WinForms應用程序中使用Web安全模塊? – nunespascal

+1

'HttpContext'是一個** ASP.NET **結構 - 僅存在於** web應用程序中** Winforms顯然不是..... –

+0

@nunespascal否,尚未 –

回答

1

不需要winforms遵循會員供應商生命週期。如果您在winforms中使用用於web請求的dll,則不會設置HttpContext。 HttpContext引用當前的web請求,這在winforms中顯然缺少。

你可以分開你的認證業務邏輯,並把它放在一個不同的DLL。您可以在Windows應用程序和您的成員資格提供程序中引用此庫。

如果您必須通過網絡對您的用戶進行身份驗證,則在Windows應用程序中執行此操作的方法是查詢Web服務。然後你可以在服務器端使用你的SimpleMembershipProvider。

+0

可能是一個WCF服務應用程序嗎? –

+0

如果你決定編寫一個web服務,你肯定可以使用WCF。 – nunespascal

相關問題