是否有 Membership.ValidateUser() 替代方案仅检查用户名密码组合?

发布于 2024-12-23 09:16:53 字数 103 浏览 5 评论 0原文

是否有 Membership.ValidateUser 替代方案仅检查用户名密码组合?换句话说,有没有办法可以告诉我用户名和密码是正确的,但用户无效?在实施注册确认电子邮件时,它可能会很方便。

Is there a Membership.ValidateUser alternative that only checks username password combo? In other words, is there a way I can tell that the username and password are correct but the user is not valid? It may come handy when implementing confirmation email for registrations.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若有似无的小暗淡 2024-12-30 09:16:53

是的,

只需在用户数据存储中进行查找即可。

例如。

public static string IsUserValid(string username, string password)
{
    if(!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password))
    {

         var allIsGood = true;
         // query user datastore for username and password
         // change allIsGood to true or false based on result

         return allIsGood;
    }

    return false;
}

编辑:

对于我使用的加密密码和 MD5 哈希。所以这个帮助器类可能会有所帮助

#region Usings

using System.Security.Cryptography;
using System.Text;

#endregion

namespace Shared.Tools
{
public static class Crypto
{
    public static string CalculateMd5Hash(string input)
    {
        // step 1, calculate MD5 hash from input
        var md5 = System.Security.Cryptography.MD5.Create();
        var inputBytes = Encoding.ASCII.GetBytes(input);
        var hash = md5.ComputeHash(inputBytes);

        // step 2, convert byte array to hex string
        var sb = new StringBuilder();
        for (var i = 0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("x2"));
        }
        return sb.ToString();
    }

    public static string GetHashValue(this MD5 hash, string value)
    {
        return GetHashValue(hash, Encoding.UTF8.GetBytes(value));
    }

    public static string GetHashValue(this MD5 hash, byte[] value)
    {
        var data = hash.ComputeHash(value);
        var md5 = "";
        for (var i = 0; i < data.Length; i++)
        {
            md5 += data[i].ToString("x2").ToLowerInvariant();
        }

        return md5;
    }

    public static string MD5(this string value)
    {
        var algorithm =
            System.Security.Cryptography.MD5.Create();

        var data = Encoding.ASCII.GetBytes(value);
        data = algorithm.ComputeHash(data);
        var md5 = "";
        for (var i = 0; i < data.Length; i++)
        {
            md5 += data[i].ToString("x2").ToLower();
        }

        return md5;
    }
}
}

,您可能会为您的测试方法执行类似的操作,

public static string IsUserValid(string username, string password)
{
    // using Linq to Entities/Sql
    return  YourDataContext
            .Users
            .Where(u => u.UserName == username
                     && u.Password == password.MD5)
            .FirstOrDefault() != null;
}

我还没有编译或测试它,但它应该为您提供您所追求的要点。

Yes,

simply do a lookup in the user datastore.

eg.

public static string IsUserValid(string username, string password)
{
    if(!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password))
    {

         var allIsGood = true;
         // query user datastore for username and password
         // change allIsGood to true or false based on result

         return allIsGood;
    }

    return false;
}

EDIT:

for the encrypted password I use and MD5 Hash. so this helper class might be helpful

#region Usings

using System.Security.Cryptography;
using System.Text;

#endregion

namespace Shared.Tools
{
public static class Crypto
{
    public static string CalculateMd5Hash(string input)
    {
        // step 1, calculate MD5 hash from input
        var md5 = System.Security.Cryptography.MD5.Create();
        var inputBytes = Encoding.ASCII.GetBytes(input);
        var hash = md5.ComputeHash(inputBytes);

        // step 2, convert byte array to hex string
        var sb = new StringBuilder();
        for (var i = 0; i < hash.Length; i++)
        {
            sb.Append(hash[i].ToString("x2"));
        }
        return sb.ToString();
    }

    public static string GetHashValue(this MD5 hash, string value)
    {
        return GetHashValue(hash, Encoding.UTF8.GetBytes(value));
    }

    public static string GetHashValue(this MD5 hash, byte[] value)
    {
        var data = hash.ComputeHash(value);
        var md5 = "";
        for (var i = 0; i < data.Length; i++)
        {
            md5 += data[i].ToString("x2").ToLowerInvariant();
        }

        return md5;
    }

    public static string MD5(this string value)
    {
        var algorithm =
            System.Security.Cryptography.MD5.Create();

        var data = Encoding.ASCII.GetBytes(value);
        data = algorithm.ComputeHash(data);
        var md5 = "";
        for (var i = 0; i < data.Length; i++)
        {
            md5 += data[i].ToString("x2").ToLower();
        }

        return md5;
    }
}
}

and you might do something like this for your test method

public static string IsUserValid(string username, string password)
{
    // using Linq to Entities/Sql
    return  YourDataContext
            .Users
            .Where(u => u.UserName == username
                     && u.Password == password.MD5)
            .FirstOrDefault() != null;
}

I haven't compiled or tested this but it should give you the gist of what you are after.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文