C# WCF和SimpleMembershipProvider。如何管理用户?

C# WCF和SimpleMembershipProvider。如何管理用户?,c#,asp.net,.net,wcf,C#,Asp.net,.net,Wcf,我有一个ASP.NET MVC 4应用程序,它具有基于SimpleMebershipProvider的用户身份验证。使用Ms SQL数据库。一切正常。我可以注册新用户、登录、注销等 问题是,我想创建Windows窗体应用程序,它可以连接到服务器,在传递凭据后,它可以验证数据库中是否存在用户(通过MVC注册),如果存在,则执行一些操作,例如更改用户名或密码。我的想法是使用WCF服务库。我知道WCF的基本思想,或者至少我希望如此:)我知道有可能对用户进行身份验证 我在网上搜索,但我没有找到如何使用S

我有一个ASP.NET MVC 4应用程序,它具有基于SimpleMebershipProvider的用户身份验证。使用Ms SQL数据库。一切正常。我可以注册新用户、登录、注销等

问题是,我想创建Windows窗体应用程序,它可以连接到服务器,在传递凭据后,它可以验证数据库中是否存在用户(通过MVC注册),如果存在,则执行一些操作,例如更改用户名或密码。我的想法是使用WCF服务库。我知道WCF的基本思想,或者至少我希望如此:)我知道有可能对用户进行身份验证

我在网上搜索,但我没有找到如何使用SimpleMebership提供程序实现这一点。我也试着自己编写WCF服务库,我创建了下面这样的东西,但它不起作用。当我测试并放置错误的凭据时,它返回字符串“bad credentials”,这是好的。但是,当我键入有效凭据时,会在第行显示错误“NullReference exception”:

if(WebSecurity.Login(用户名、密码、持久cookie:false))

我认为它也不安全:/

有人能告诉我怎么做或者我做错了什么吗??或者也许有比WCF更好的解决方案

服务1.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>

将以下内容添加到WCF服务所在主机的Web.config中:

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

将以下内容添加到WCF服务所在主机的Web.config中:

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


请查看类似问题的答案:请查看类似问题的答案: