验证AD的凭据

时间:2016-10-31 13:31:04

标签: c# asp.net .net

可能是脑力,但是, 每当我尝试在我的网络表单中针对广告验证我的凭据时,它总是返回true(已批准),为什么会这样?这只是一个测试,以确保每当我点击"提交"按钮:

<!DOCTYPE html>
    <html>
    <head>
        <title>Test</title>
        <script type="text/javascript">
        function kwadraat(){
            var text = "", i,
                t = parseInt(text2.value, 10),
                demo = document.getElementById("demo"),
                numm = t;
            if (numm > 20) {
                demo.innerHTML = 'Too Big!!!';
                return;
            }
            for (i = 1; i <= numm; i++) {
                text += "The number is " + i + "="+ (i*i) +"<br>";
            }
            demo.innerHTML = text;
        }
        </script>
    </head>
    <body>
    Test<br>
    <input type="text" id="text2">
        <p id="demo"></p>
        <button onClick="kwadraat()">Test</button>
    </body>
    </html>

1 个答案:

答案 0 :(得分:2)

您的if语句正在检查名为IsValid的对象的真值。您创建的bool名为isValid。请注意,一个以资本开头,另一个则不是。

bool isValid将不会存在于您的使用块之外,因为它在内部声明。将isValid移到using语句之外,如下所示:

protected void Button1_Click(object sender, EventArgs e)
{
    // Username and password text
    var username1 = uniloginTextBox.Text;
    var password1 = passwordTextBox.Text;

    // isValid defaults to false
    bool isValid = false;

    // Check credentials against AD
    using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "cv.local"))
    {
        // Set value for isValid
        isValid = pc.ValidateCredentials(username1, password1, ContextOptions.Negotiate);
    }

    // Achieves the same as your if statement
    Label.Text = isValid ? "Approved" : "Denied";
}