2017-07-02 89 views
0

我實現了一個插件(使用pGina軟件),允許用戶通過掃描NFC標籤在其計算機中驗證用戶名/密碼。在C中調用靜態方法#

我使用了一個我發現名爲CSharp PC/SC Wrapper for .NET的程序來讀取標籤ID。每次掃描標籤時,程序都會將ID寫入文本文件,並檢查ID是否與字符串上設置的ID相同。

if (userInfo.Username.Contains("hello") && userInfo.Password.Contains("pGina") 
    && text.Equals("UID = 0x04 82 EC BA 7A 48 80")) 

該插件設置爲查找讀取ID(PC/SC Wrapper)的.exe文件。一切正常。但是,我不認爲讀者程序是在不同的文件中。我希望一切都在插件文件中。

我創建了一個方法並從執行標籤ID(runme())讀取的包裝中複製代碼,但我不知道如何用方法I替換調用.exe文件的行創建

ProcessStartInfo ps = new ProcessStartInfo(@"C:\Users\Student\Desktop\CSharpPCSC\CSharpPCSC\ExamplePCSCReader\bin\Release\ExamplePCSCReader.exe");

有什麼建議?我是新的C#

下面是我用含有讀取ID

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using pGina.Shared.Types; 
using log4net; 
using System.IO; 
using System.Diagnostics; 
using GS.PCSC; 
using GS.Apdu; 
using GS.SCard; 
using GS.Util.Hex; 
using System.Threading; 

namespace HelloPlugin 
{ 
    public class PluginImpl : pGina.Shared.Interfaces.IPluginAuthentication 
    { 
     private ILog m_logger; 

     private static readonly Guid m_uuid = new Guid("CED8D126-9121-4CD2-86DE-3D84E4A2625E"); 

     public PluginImpl() 
     { 
      m_logger = LogManager.GetLogger("pGina.Plugin.HelloPlugin"); 
     } 

     public string Name 
     { 
      get { return "Hello"; } 
     } 

     public string Description 
     { 
      get { return "Authenticates users with 'hello' in the username and 'pGina' in the password"; } 
     } 

     public Guid Uuid 
     { 
      get { return m_uuid; } 
     } 

     public string Version 
     { 
      get 
      { 
       return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(); 
      } 
     } 

     public void Starting() 
     { 

     } 

     public void Stopping() { } 

     public BooleanResult AuthenticateUser(SessionProperties properties) 
     { 

      UserInformation userInfo = properties.GetTrackedSingle<UserInformation>(); 

      ProcessStartInfo ps = new ProcessStartInfo(@"C:\Users\Student\Desktop\CSharpPCSC\CSharpPCSC\ExamplePCSCReader\bin\Release\ExamplePCSCReader.exe"); 
      Process.Start(ps); 
      Thread.Sleep(2000); 
      string text = File.ReadAllText(@"C:\Users\Student\Desktop\text.txt", Encoding.UTF8); 
      text = text.Trim(); 

      if (userInfo.Username.Contains("hello") && userInfo.Password.Contains("pGina") && text.Equals("UID = 0x04 82 EC BA 7A 48 80")) 
      { 
       // Successful authentication 
       m_logger.InfoFormat("Successfully authenticated {0}", userInfo.Username); 
       return new BooleanResult() { Success = true }; 
      } 
      // Authentication failure 
      m_logger.ErrorFormat("Authentication failed for {0}", userInfo.Username); 
      return new BooleanResult() { Success = false, Message = "Incorrect username or password." }; 
     } 

     static void runme() 
     { 
      ConsoleTraceListener consoleTraceListener = new ConsoleTraceListener(); 
      Trace.Listeners.Add(consoleTraceListener); 

      PCSCReader reader = new PCSCReader(); 
      string cardid = ""; 

      try 
      { 
       reader.Connect(); 
       reader.ActivateCard(); 

       RespApdu respApdu = reader.Exchange("FF CA 00 00 00"); // Get NFC Card UID ... 
       if (respApdu.SW1SW2 == 0x9000) 
       { 
        Console.WriteLine("UID = 0x" + HexFormatting.ToHexString(respApdu.Data, true)); 
        cardid = "UID = 0x" + HexFormatting.ToHexString(respApdu.Data, true); 
        cardid = cardid.Trim(); 
       } 
      } 
      catch (WinSCardException ex) 
      { 
       Console.WriteLine(ex.WinSCardFunctionName + " Error 0x" + 
            ex.Status.ToString("X08") + ": " + ex.Message); 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
      } 
      finally 
      { 
       string path = @"C:\Users\Student\Desktop\text.txt"; 
       string text2write = cardid; 

       System.IO.StreamWriter writer = new System.IO.StreamWriter(path); 
       writer.Write(text2write); 
       writer.Close(); 

       reader.Disconnect(); 
       Environment.Exit(0); 
       Console.WriteLine("Please press any key..."); 
       Console.ReadLine(); 
      } 
     } 
    } 
} 

回答

1

您已經創建了一個名爲PluginImpl類,並在類中聲明的代碼的方法插件代碼方法runme。要從任何地方調用該方法,您需要編寫PluginImpl.runme()

由於您已將課程放在命名空間HelloPlugin中 - 如果調用的*.cs文件位於不同的命名空間中,您需要在頂部有using HelloPlugin指令。

就這樣!

這可能是我誤解了你的問題,如果是的話請重新提出你的問題,並給我發一條評論。


如果要替換的方法調用的行

ProcessStartInfo ps = new ProcessStartInfo(
     @"C:\Users\Student\Desktop\CSharpPCSC\CSharpPCSC\" 
     +"ExamplePCSCReader\bin\Release\ExamplePCSCReader.exe"); 

相反,你想是這樣的

ProcessStartInfo ps = runme(); 

因爲你是從類中調用的靜態方法,你不需要前綴PluginImpl.

好吧,現在它會抱怨runme不會返回ProcessStartInfo。你將需要改變runme,以便它可以。 ProcessStartInfo的任何子類都可以。

static ProcessStartInfo runme() 
{ 
    // ... Some code 

    ProcessStartInfo toReturn = new ProcessStartInfo(//... 
    ); 

    // ... More code 

    return toReturn; 
} 
+0

一旦我使用PluginImpl.runme(),我該如何替換ProcessStartInfo()來調用該方法。 ProcessStartInfo ps = new ProcessStartInfo(@「C:\ Users \ Student \ Desktop \ CSharpPCSC \ CSharpPCSC \ ExamplePCSCReader \ bin \ Release \ ExamplePCSCReader.exe」); –

+0

@mj_h我希望我最近的編輯能夠解決您的問題。如果這還不清楚,讓我知道。 –