我正在运行一个更改用户密码的 ASP.NET 应用程序。PasswordException “指定的网络密码不正确。” 每次调用 ChangePassword 方法时都会抛出,即使当前密码已经过验证。
如果我输入的当前密码无效,则会引发异常。这是预期的结果。
如果我输入一个有效的当前密码,则会抛出异常,但密码仍然会被更改(我已经测试过在更改后立即验证它)。
代码非常简单:
var context = new PrincipalContext(ContextType.Domain, "domain.net");
var valid = context.ValidateCredentials(username, oldPassword);
var userPrincipal = UserPrincipal.FindByIdentity(context, username);
userPrincipal.ChangePassword(oldPassword, newPassword);
Run Code Online (Sandbox Code Playgroud)
这导致每次都抛出以下异常,无论当前密码是否正确:
System.DirectoryServices.AccountManagement.PasswordException: The specified network password is not correct. (Exception from HRESULT: 0x80070056) ---> System.Runtime.InteropServices.COMException: The specified network password is not correct. (Exception from HRESULT: 0x80070056)
--- End of inner exception stack trace ---
at System.DirectoryServices.AccountManagement.SDSUtils.ChangePassword(DirectoryEntry de, String oldPassword, String newPassword)
at System.DirectoryServices.AccountManagement.ADStoreCtx.ChangePassword(AuthenticablePrincipal p, String oldPassword, String newPassword) …
Run Code Online (Sandbox Code Playgroud) 我有一个中间类,为我需要身份验证的所有页面扩展System.Web.UI.Page.该类主要进行自定义身份验证处理.
当访问权限不足的用户尝试访问页面时,我尝试将用户重定向回登录页面,同时防止执行任何其他页面事件(即Page_load).想到的第一个解决方案是Response.Redirect的默认实现.当然,这样做的缺点是抛出ThreadAbortExceptions的可能性.
所以我的问题是这样的:在页面生命周期中何时(如果有的话)执行Response.Redirect()实际上是安全的,而不会抛出ThreadAbortException?
public class CustomPage : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (!IsValid())
Response.Redirect("login.aspx", true);
}
}
Run Code Online (Sandbox Code Playgroud) 我们有一个现有的可公开访问的Web应用程序,其中包含用户控件,数据访问库,图形等.我们希望创建一个新的安全部分,访问一些已有的资源.
最初我们创建了网站的新部分作为虚拟目录(我们希望)允许我们访问父网站的资源.我们将相应的位置信息添加到基本web.config(身份验证和授权),但我们继续看到以下错误"解析器错误消息:使用注册为allowDefinition ='MachineToApplication'的部分超出应用程序级别是错误的.虚拟目录未配置为IIS中的应用程序可能导致错误."
为了响应该错误,我们将目录创建为新应用程序.这允许我们正确地进行身份验证,但缺点是无法访问父目录中的任何资源(因为它超出了应用程序范围).
有没有办法保护网站的新部分,同时利用现有的资源?