[英]How do I get the username and password from a URL
用户名和密码来自如下网址:
https://theuser:thepassword@localhost:20694/WebApp
创建一个Uri
,然后获取UserInfo
属性:
var uri = new Uri("https://theuser:thepassword@localhost:20694/WebApp");
Console.WriteLine(uri.UserInfo); // theuser:thepassword
如有必要,您可以在:
拆分,如下所示:
var uri = new Uri("https://theuser:thepassword@localhost:20694/WebApp");
var userInfo = uri.UserInfo.Split(':');
Console.WriteLine(userInfo[0]); // theuser
Console.WriteLine(userInfo[1]); // thepassword
请注意,如果要在ASP.NET请求的上下文中获取当前用户,最好使用提供的API,例如HttpContext.User
:
var userName = HttpContext.Current.User.Identity.Name;
或者,如果这是Web表单,则:
protected void Page_Load(object sender, EventArgs e)
{
Page.Title = "Home page for " + User.Identity.Name;
}
else
{
Page.Title = "Home page for guest user.";
}
}
至于密码,建议您不要在验证用户身份后立即尝试处理密码。
var str = @"https://theuser:thepassword@localhost:20694/WebApp";
var strsplit = str.Split(new char[] {':', '@', '/'};
var user = strsplit[1];
var password = strsplit[2];
您可以通过以下方法获取当前的uri
string uri = HttpContext.Current.Request.Url.AbsoluteUri; //as u targeted .net
然后将其解析为普通字符串。
您将收到以下字符串: https://theuser:thepassword@localhost:20694/WebApp
通过将字符串分成多个部分,您可以获取要搜索的信息。
var uri = "https://theuser:thepassword@localhost:20694/WebApp";
var currentUriSplit = uri.Split(new [] { ':', '@', '/' });
var user = currentUriSplit[3];
var password = currentUriSplit[4];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.