在.NET HTTP认证NET、HTTP

2023-09-05 00:34:41 作者:蓦然回首

是否有可能产生等同于下面code一个.NET?

Is it possible to create a .NET equivalent to the following code?

<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
    echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
    echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}
?>

我希望能够定义一个静态的用户名/密码在web.config中也是如此。这是很容易做到在PHP中,没有看到任何解释如何做到这一点的MSDN

I would like to be able to define a static user/password in the web.config as well. This is very easy to do in PHP, haven't seen anything explaining how to do this in MSDN.

我要的是这样的:

推荐答案

要实现一样在PHP code将作为直接通过的 Reponse.AppendHeader()。

The easiest way to achieve the same as with the PHP code would be to directly send the same headers via Reponse.AppendHeader().

不过我建议你阅读 ASP.NET窗体身份验证教程。