【发布时间】:2016-04-25 19:54:48
【问题描述】:
我正在构建一个自定义 IIS FTP 身份验证器,旨在从 SQL Server 表进行身份验证。该表具有用户名、密码和主目录,这些都是从查询中返回的。所有这些似乎在验证器中都可以正常工作,但是当我尝试实现主目录提供程序时,我从来没有进入我指定的文件夹 - 似乎实际上并没有调用提供程序。同样,如果我尝试实现一个后期处理来提供上传文件的自定义处理,那也不会触发。我很困惑,因为我认为我已经正确注册了所有内容,否则身份验证方法将无法运行 - 那么为什么其他两个不执行?
我已经缩减了我的代码,所以我只是写入一个文本文件,这样我就可以判断该方法是否正在触发。身份验证有效,其他则无效。
using System;
using System.IO;
using Microsoft.Web.FtpServer;
namespace MyCustomFtpExtension
{
public class DatabaseAuthenticator : BaseProvider,
IFtpAuthenticationProvider,
IFtpRoleProvider, IFtpHomeDirectoryProvider, IFtpPostprocessProvider,
IFtpLogProvider
{
private readonly string _logfile = Path.Combine(@"c:\test", "logs", "FtpExtension.log");
public bool AuthenticateUser(string sessionId, string siteName, string userName, string userPassword,
out string canonicalUserName)
{
canonicalUserName = userName;
var result = DatabaseHelper.Authenticate(userName, userPassword);
if (result)
{
LogMessage("Login Success: " + userName); //this message appears
}
else
{
LogMessage("Login Failure: " + userName);
}
return result;
}
string IFtpHomeDirectoryProvider.GetUserHomeDirectoryData(
string sessionId,
string siteName,
string userName)
{
LogMessage("In ftp home directory"); //this message never appears
return @"c:\temp\test";
}
public FtpProcessStatus HandlePostprocess(FtpPostprocessParameters postProcessParameters)
{
LogMessage("Running Post Process"); //this message never appears
return FtpProcessStatus.FtpProcessContinue;
}
public bool IsUserInRole(string sessionId, string siteName, string userName, string userRole)
{
return true; // I don't care about this - if they authenticate, that's all I need.
}
//to keep the sample short I took out the log provider - it was copy / paste from Microsoft's example
//this is a quick and dirty output so I can see what's going on (which isn't much)
private void LogMessage(string logEntry)
{
using (var sw = new StreamWriter(_logfile, true))
{
// Retrieve the current date and time for the log entry.
var dt = DateTime.Now;
sw.WriteLine("{0}\t{1}\tMESSAGE:{2}",
dt.ToShortDateString(),
dt.ToLongTimeString(),
logEntry);
sw.Flush();
}
}
}
}
【问题讨论】: