如何使用“:”分隔用户名和密码? [英] How to seprate username and password with ":"?
问题描述
嗨朋友们,
我已经制作了一个应用程序,它将从以下表格的文本文件中获取用户名和密码
用户名:密码
然后这些细节将添加到组合框中。
我想在:之前将该文本(用户名)设置为登录表单的用户名字段,而:之后的文本(密码)应该输入密码字段。
请帮忙怎么做?
这真是一个糟糕的主意,但是......
string input = 用户名:密码跨度>;
string [] parts = input.Split(' :跨度>);
if (parts.Length > = 2 )
{
Console.WriteLine( 用户名:{0} ,部分[ 0 ]);
Console.WriteLine( 密码:{0},部分[ 1 跨度>]);
}
这是非常非常不安全的 - 所以不要使用任何真正的密码进行测试(或现实世界)数据,如果你打算这样做的话)
如果有人拿到文本文件,他们可以拥有你的整个密码列表......
有两种方法可以做到这一点
1)使用上面的解决方案2中所示的拆分功能
2)使用左侧内置函数,如下所示(这是VB6示例代码):
Dim str As String
Dim uname As 字符串
Dim pwd 作为 字符串
str = :usern amepassword
如果 InStr( 1 ,str, :)> 0 然后
uname =左(str,InStr( 1 ,str, :) - 1 )
pwd =右(str,Len(str) - InStr( 1 ,str, :))
否则
uname = str
pwd =
结束 如果
您还可以将正则表达式与组名称一起使用。
// 此表达式允许密码部分为空。
// 更改* in( ?< pwd>。*)到+,要求至少一个字符
System.Text.RegularExpressions.Regex expr =
new System.Text。 RegularExpressions.Regex( @ ^(?< user> [\\\]] +?\\ S *:\s *
(小于; PWD>的 * 吗?)。
Hi Friends,
I have made an app which will get username and password from a text file in the following form
username:password
These details then will be added to a combobox.
I want to make that text (username) before ":" should go in username field of the login form and text (password) after ":" should go in password field.
Please help how to do it?
That really is a poor idea, but...
string input = "username:password"; string[] parts = input.Split(':'); if (parts.Length >= 2) { Console.WriteLine("Username : {0}", parts[0]); Console.WriteLine("Password : {0}", parts[1]); }
It's very, very insecure - so don't use any of your real passwords for testing (or real world data, if you are going to do that with it)
If anyone gets their hands on the text file, they could have your entire password list...
There are two ways of doing it
1) Using the split function as shown in solution 2 above
2) using the left right inbuilt functions as shown below (THIS IS VB6 Sample code):
Dim str As String Dim uname As String Dim pwd As String str = ":usernamepassword" If InStr(1, str, ":") > 0 Then uname = Left(str, InStr(1, str, ":") - 1) pwd = Right(str, Len(str) - InStr(1, str, ":")) Else uname = str pwd = "" End If
You can also use regular expression with group names.
// This expression allows the password part to be empty. // Change the * in (?<pwd>.*) to a +, to require at least one character System.Text.RegularExpressions.Regex expr = new System.Text.RegularExpressions.Regex(@"^(?<user>[\S\s]+?)\s*:\s*(?<pwd>.*)
这篇关于如何使用“:”分隔用户名和密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!