有没有一种方法可以对 ASP.NET 网站进行网络保护,使其要求用户输入用户名/密码才能查看?
我被告知这应该放在 web.config 文件中,但不确定如何执行此操作,或者应该放在其中的内容。非常感谢任何帮助。
最佳答案
如果是内网场景那就很简单了。将其添加到您的 web.config
<configuration>
<system.web>
<authentication mode=”Windows” />
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
最快的互联网应用方式:
<authentication mode="Forms">
<forms loginUrl="~/login.aspx">
<credentials>
<user name="user" password="password" />
<user name="user2" password="password2" />
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
您还需要一个新页面login.aspx,其中包含一个用户可以输入其凭据的表单。在代码隐藏页面上,您需要以下代码:
string username = textBoxUsername.Text;
string password = textBoxPassword.Text;
if (FormsAuthentication.Authenticate(username, password))
FormsAuthentication.RedirectFromLoginPage(username, false);
关于asp.net - Web 保护 ASP.NET 网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8758169/