FubarDevelopment / FtpServer

Portable FTP server written in .NET
http://fubardevelopment.github.io/FtpServer/
MIT License
482 stars 163 forks source link

FTPServer with with custom credentials(UserName and Password) #19

Closed abunoman2003 closed 6 years ago

abunoman2003 commented 7 years ago

Hi Would anyone please share minimal working code for FTP Server with User Name and Password? Thank you

AleksandreSukh commented 7 years ago

Can you explain it more specifically what do you need?

abunoman2003 commented 7 years ago

@sandrinio1 I need Start FTP Server with specifying user name and password. So fpt client can access to server using that user name and password. Eg. FTP Server start with user Name : user1 and password: pass4User1 and client will be able to connect using same credentials.

Jasonkingsmill commented 7 years ago

The way I have implemented it is like the below. Be aware this only allows a single username/password combination. It should be pretty straight forward to take a Dictionary of user accounts if required.

public class BasicUserMembershipProvider : IMembershipProvider
    {
        private string _username;
        private string _password;

        public BasicUserMembershipProvider(string username, string password)
        {
            _username = username;
            _password = password;
        }

        public MemberValidationResult ValidateUser(string username, string password)
        {
            if(username != null && username.Equals(this._username, StringComparison.InvariantCultureIgnoreCase))
            {
                if (password != null && password.Equals(password))
                    return new MemberValidationResult(MemberValidationStatus.AuthenticatedUser, new FtpUser(username));
            }
            return new MemberValidationResult(MemberValidationStatus.InvalidLogin);
        }
    }

You would use it like:

var membershipProvider = new BasicUserMembershipProvider(username, password);           
var fsProvider = new DotNetFileSystemProvider(rootPath, false);
var ftpServer = new FtpServer(fsProvider, membershipProvider, serverAddress);
ftpServer.Start();
SinnedB commented 4 years ago

If anyone else needs it:

services.AddFtpServer(builder => builder .UseDotNetFileSystem() .Services.AddSingleton<IMembershipProvider, CustomMemberShipProvider>());

robokamran commented 4 years ago

this should do the trick (be sure to define HostedFtpService). put these in the ConfigureServices:

services.AddFtpServer(opt => opt.UseDotNetFileSystem());
services.AddSingleton<IMembershipProvider, CustomMembershipProvider>();
services.Configure<FtpServerOptions>(opt => opt.ServerAddress = "*");
services.AddHostedService<HostedFtpService>();

and define this separately:

public class CustomMembershipProvider : IMembershipProvider
{
    public Task<MemberValidationResult> ValidateUserAsync(string username, string password)
    {
        if (username != "melika" || password != "jesica")
            return Task.FromResult(new MemberValidationResult(MemberValidationStatus.InvalidLogin));

        var claims = new[]
        {
            new Claim(ClaimsIdentity.DefaultNameClaimType, username),
            new Claim(ClaimsIdentity.DefaultRoleClaimType, username),
            new Claim(ClaimsIdentity.DefaultRoleClaimType, "user"),
        };

        var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "custom"));
        return Task.FromResult(new MemberValidationResult(MemberValidationStatus.AuthenticatedUser, user));
    }
}
PlayXboxtion963 commented 2 years ago
public class FTP
    {
        public string setpassword { get; set; } = "100";
        public string ippub { get; set; } = "100";
        public void Startftp()
        {
            string ipx="192.168.0.1";
            string localIP = string.Empty;
            using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0))
            {
                socket.Connect("8.8.8.8", 65530);
                IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint;
                localIP = endPoint.Address.ToString();
                ipx = localIP;
            }
            ippub= ipx;
            System.Diagnostics.Debug.WriteLine(ipx);
            // Setup dependency injection
            var services = new ServiceCollection();
            // use %TEMP%/TestFtpServer as root folder
            services.Configure<DotNetFileSystemOptions>(opt => opt
                .RootPath = System.IO.Directory.GetCurrentDirectory()+ @"\workfloader");
            services.AddFtpServer(builder => builder
                .UseDotNetFileSystem() 
                );
            services.Configure<FtpServerOptions>(opt => opt
            .ServerAddress = ipx);
            services.Configure<FtpServerOptions>(opt => opt
              .Port = 23235);
            CustomMembershipProvider mCustomMembershipProvider = new CustomMembershipProvider();
            **mCustomMembershipProvider.CustomPassword = setpassword;
            services.AddSingleton<IMembershipProvider, CustomMembershipProvider>(CustomMembershipProvider=> mCustomMembershipProvider);**

            var serviceProvider = services.BuildServiceProvider();
            // Initialize the FTP server
            var ftpServerHost = serviceProvider.GetRequiredService<IFtpServerHost>();

            // Start the FTP server
            try
            {
                ftpServerHost.StartAsync().Wait();
            }catch (Exception)
            {
                MessageBox.Show("StartFail");
            }

        }

    }
    public class CustomMembershipProvider : IMembershipProvider
    {

        public String CustomPassword { get; set; } ="100";

        public Task<MemberValidationResult> ValidateUserAsync(string username, string password)
        {
            if (username != "_yourownname_" || password != CustomPassword)
                return Task.FromResult(new MemberValidationResult(MemberValidationStatus.InvalidLogin));
            var claims = new[]
            {
            new Claim(ClaimsIdentity.DefaultNameClaimType, username),
            new Claim(ClaimsIdentity.DefaultRoleClaimType, username),
            new Claim(ClaimsIdentity.DefaultRoleClaimType, "user"),
        };

            var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "custom"));
            return Task.FromResult(new MemberValidationResult(MemberValidationStatus.AuthenticatedUser, user));
        }
    }
}
Then just use 
FTP mftp = new FTP();
mftp.setpassword=//which you CustomPassword;
mftp.startftp();