我正在尝试这样做:
string username = httpContext.User.Identity.Name.ToString();
if (u.Equals(username))
{
authorise = true;
}域名和用户名完全相同,字母对应,反斜杠表示反斜杠,大写字母表示大写等。但是变量authorise仍然是假的。不知道为什么。我开始怀疑是否有一种更好的方法可以做到这一点。
发布于 2014-02-28 23:36:18
这可能是某种类型的不匹配。我会在这里写一个单元测试来验证你的假设。
反斜杠可能会有不同的转义方式。您可以尝试在没有这些的情况下进行匹配。
function string CleanAndTrim(string user){
var cleaned = user.Replace(@"\", "");
cleaned = user.Replace(@"/", "")
return cleaned.ToLower().Trim();
}
if(CleanAndTrim(u)==CleanAndTrim(username){
authorise = true;
}发布于 2014-02-28 23:42:04
为什么要在字符串上调用.ToString()?为什么要使用if语句来设置布尔值?--直接分配布尔值。试试这个:
string username = httpContext.User.Identity.Name;
authorise = u.Equals(username);我现在不会担心最佳实践,因为您需要首先了解基础知识。
https://stackoverflow.com/questions/22098928
复制相似问题