1

SimpleMemebershipProvider に基づくユーザー認証を使用する ASP.NET MVC 4 アプリケーションがあります。Ms SQL データベースを使用します。すべて正常に動作します。新規ユーザーの登録、ログイン、ログアウトなどを行うことができます。

問題は、サーバーに接続できる Windows フォーム アプリケーションを作成し、資格情報を渡した後、ユーザーがデータベースに存在する (MVC を介して登録されている) かどうかを検証し、存在する場合は、ユーザー名やパスワードの変更などを行うことです。私の考えは、WCF サービス ライブラリを使用することです。私はWCFの基本的な考え方を知っているか、少なくともそう願っています:)ユーザーを認証する可能性があることを知っています。

私はウェブを検索していましたが、simplemebership プロバイダーでこれを行う方法が見つかりませんでした。また、自分で WCF Service ライブラリを作成しようとしましたが、以下のようなものを作成しましたが、機能しません。テスト中に間違った資格情報を入力すると、「bad credentials」という文字列が返されます。ただし、有効な資格情報を入力すると、次の行に「NullReference 例外」というエラーが表示されます。

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

どちらも安全ではないと思います:/

誰かがこれの方法や私が間違っていることを説明できますか?? それとも、WCF よりも優れたソリューションがあるのでしょうか。

Service1.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>
4

1 に答える 1