简体   繁体   English

ASP.Net - 如何在使用Login Control登录前加密密码

[英]ASP.Net - How to Encrypt password before login while using Login Control

I'm using the ASP.Net Login control to login a web application. 我正在使用ASP.Net Login控件登录Web应用程序。 When the user submits the form, I want to call a function to encrypt the password before ASP.Net sends the information for authentication. 当用户提交表单时,我想调用一个函数来加密密码,然后ASP.Net发送信息进行身份验证。

I tried to add a Customer Validator on the password field and if the password is not empty, the Password.Text will be replaced with the encrypted value. 我尝试在密码字段上添加Customer Validator,如果密码不为空,则Password.Text将替换为加密值。 However, it seems ASP.Net still sent the original password for authentication. 但是,似乎ASP.Net仍然发送了原始密码进行身份验证。

I also tried adding a onClick event on the login button to encrypt the password but ASP.Net still sent the original password for authentication. 我还尝试在登录按钮上添加onClick事件来加密密码,但ASP.Net仍然发送了原始密码进行身份验证。

Is there a way to do this? 有没有办法做到这一点? Thank you! 谢谢!

UPDATE: 更新:

I'm sorry for not making this clear. 对不起,我很抱歉。 What I need is to encrypt the password at Server Side. 我需要的是加密服务器端的密码。

I'm not using ASP.Net Membership to encrypt or hash the password while registering a user. 在注册用户时,我没有使用ASP.Net Membership来加密或散列密码。 The passwordFormat property has been set to "Clear". passwordFormat属性已设置为“Clear”。

What I am doing is: 我在做的是:

  1. While a new user registers, I use a customized function to encrypt the password and save it to database. 当新用户注册时,我使用自定义函数来加密密码并将其保存到数据库。
  2. When a user tries to login, I want to use the same function to encrypt the password entered by the user and let ASP.Net to authenticate the user. 当用户尝试登录时,我想使用相同的功能来加密用户输入的密码,并让ASP.Net对用户进行身份验证。

The problem I'm having is I can't find a way to call the encrypt function before ASP.Net initiate the authentication process. 我遇到的问题是我在ASP.Net启动身份验证过程之前找不到调用加密函数的方法。

Hope this makes sense. 希望这是有道理的。 Thank you. 谢谢。

Allen 艾伦

You were definitely on the right track with adding the OnClick event. 你肯定在添加OnClick事件的正确轨道上。 If you are trying to do the encryption client-side then you will need to use the OnClientClick event instead (OnClick happens server-side and OnClientClick happens client-side). 如果您尝试进行加密客户端,则需要使用OnClientClick事件(OnClick发生在服务器端,OnClientClick发生在客户端)。 I initially assumed you were using it to call a client-side javascript function that does the encryption? 我最初假设你用它来调用加密的客户端javascript函数?

[ EDIT ] [ 编辑 ]

However, if you are doing the encryption server-side, and using a Login control, then you might want to use the OnAuthenticate event: 但是,如果您正在使用加密服务器端并使用Login控件,那么您可能希望使用OnAuthenticate事件:

<asp:Login id="Login1" runat="server" OnAuthenticate="OnAuthenticate">
</asp:Login>

Then do your encryption here: 然后在这里进行加密:

private void OnAuthenticate(object sender, AuthenticateEventArgs e) {
    bool authenticated = false;
    String encryptedPassword = Encrypt(Login1.Password);
    authenticated = YourAuthenticationMethod(Login1.UserName, encryptedPassword );

    e.Authenticated = authenticated;
}
private bool YourAuthenticationMethod(String username, String encryptedPassword) {
    //test the encrypted password against that retrieved from your database using the username
}

Why are you trying to encrypt the password client side before sending it to the server? 为什么在将密码客户端发送到服务器之前尝试对其进行加密? That's really no more secure than sending the server your plain password. 这实际上并不比向服务器发送普通密码更安全。 The code you write to encrypt this password is viewable by anyone. 您编写的用于加密此密码的代码可供任何人查看。

On the server you should use something like this: 在服务器上你应该使用这样的东西:

public static string createPasswordHash(string pwd)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5");
    }

Sorry if I misunderstood something about the ASP.NET technology, but it should provide server side application. 很抱歉,如果我误解了ASP.NET技术,但它应该提供服务器端应用程序。 Therefore, You should not be able to use any "c# code" to encrypt transferring password. 因此,您不应该使用任何“c#代码”来加密传输密码。 If You cannot use secured HTTP (if You could, You wouldn't need to encrypt the password, because all communication would be encrypted), JavaScript is "only way". 如果您不能使用安全HTTP(如果可以,您不需要加密密码,因为所有通信都将被加密),JavaScript是“唯一的方式”。 However, if Your project contains client-side application (the one that is really installed (or run) on client's computer), You can use full c# potential of course. 但是,如果您的项目包含客户端应用程序(在客户端计算机上真正安装(或运行)的应用程序),您当然可以使用完整的c#潜力。

There is a discussion why would You need it at all. 有一个讨论为什么你需要它。 As I see no better explanation, I will try to provide my own: The encryption of password transferred via Internet is essential if you expect that someone will listen to the communication in between client's computer and the server. 由于我没有找到更好的解释,我将尝试提供自己的解释:如果您希望有人在客户端的计算机和服务器之间收听通信,则通过Internet传输的密码加密是必不可少的。 As I understand it, if user clicks on the log-in button at your site, the page he sees the form on is actually downloaded in his computer and the click only causes a transfer of data from client to server. 据我了解,如果用户点击您站点上的登录按钮,他看到该表单的页面实际上是在他的计算机中下载的,而点击只会导致数据从客户端传输到服务器。 The data is not encrypted at all and any evil Eve can listen to the transfer obtaining client's plain text password. 数据根本没有加密,任何邪恶的Eve都可以​​收听转移获取客户端的纯文本密码。
But You should be aware that even if You send encrypted password, the encryption covers only plain text password problem. 但您应该知道即使您发送加密密码,加密也只包括纯文本密码问题。 If Your server-side application expects password encrypted with a static algorithm and my only goal (as evil Eve) is to successfully log into the system, I don't actually need to know the password itself, its encrypted form will be good enough. 如果您的服务器端应用程序需要使用静态算法加密密码,并且我唯一的目标(如邪恶的Eve)是成功登录系统,我实际上并不需要知道密码本身,其加密形式将足够好。 It is quite complex problem and it depends on how much security Your connection really needs - if the costs (or Your effort) are relevant to the risk. 这是一个非常复杂的问题,它取决于您的连接确实需要多少安全性 - 如果成本(或您的努力)与风险相关。 If You are really serious with as best as possible security, You should go through some security standards. 如果你真的认真对待尽可能安全,那么你应该遵守一些安全标准。
The point about seeing Your algorithm written in JavaScript is irrelevant as far as it is well implemented (RSA is both open and easily accessible algorithm, though safe enough). 关于看到用JavaScript编写的算法的观点是无关紧要的,只要它实现得很好(RSA既开放且易于访问,但足够安全)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM