1

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

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

我在网上搜索,但我没有找到如何使用 simplememebership 提供者来做到这一点。我也尝试过自己编写 WCF 服务库,并在下面创建了类似的东西,但它不起作用。当我测试并输入错误的凭据时,它会返回字符串“坏凭据”,这很好。但是,当我输入有效凭据时,它会在线显示错误“NullReference 异常”:

if (WebSecurity.Login(UserName, password, persistCookie: 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";
            }
        }

    }
}

应用程序配置

<?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>
4

1 回答 1

1

将以下内容添加到任何托管 WCF 服务的 Web.config 中:

<system.serviceModel>
   <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
于 2013-04-01T12:46:57.400 回答