Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
abunoman2003 opened this issue Aug 12, 2017 · 6 comments
Closed

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

abunoman2003 opened this issue Aug 12, 2017 · 6 comments

Comments

@abunoman2003
Copy link

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

@AleksandreSukh
Copy link

Can you explain it more specifically what do you need?

@abunoman2003
Copy link
Author

@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
Copy link

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
Copy link

SinnedB commented Jan 7, 2020

If anyone else needs it:

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

@robokamran
Copy link

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
Copy link

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();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants