如何根据 ASP.NET 成员资格数据库手动验证用户?

发布于 2024-09-01 14:01:14 字数 1027 浏览 4 评论 0原文

我想知道如何根据现有的 asp.net 会员数据库验证用户的凭据。简而言之,我们希望提供单点登录访问。

所以我所做的就是直接连接到会员数据库并尝试对 aspnet_Membership 表运行 sql 查询:

private bool CanLogin(string userName, string password)
{
  // Check DB to see if the credential is correct
  try
    {
      string passwordHash = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
      string sql = string.Format("select 1 from aspnet_Users a inner join aspnet_Membership b on a.UserId = b.UserId and a.applicationid = b.applicationid where a.username = '{0}' and b.password='{1}'", userName.ToLowerInvariant(), passwordHash);
      using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString))
      using (SqlCommand sqlCmd = new SqlCommand(sql, sqlConn))
      {
         sqlConn.Open();
         int count = sqlCmd.ExecuteNonQuery();
         return count == 1;
       }
    }
    catch (Exception ex)
    {
        return false;
    }    
 }

问题是密码值,有谁知道密码是如何散列的吗?

I would like to know how I can verify a user's credential against an existing asp.net membership database. The short story is that we want provide single sign on access.

So what I've done is to connect directly to the membership database and tried to run a sql query against the aspnet_Membership table:

private bool CanLogin(string userName, string password)
{
  // Check DB to see if the credential is correct
  try
    {
      string passwordHash = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "SHA1");
      string sql = string.Format("select 1 from aspnet_Users a inner join aspnet_Membership b on a.UserId = b.UserId and a.applicationid = b.applicationid where a.username = '{0}' and b.password='{1}'", userName.ToLowerInvariant(), passwordHash);
      using (SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString))
      using (SqlCommand sqlCmd = new SqlCommand(sql, sqlConn))
      {
         sqlConn.Open();
         int count = sqlCmd.ExecuteNonQuery();
         return count == 1;
       }
    }
    catch (Exception ex)
    {
        return false;
    }    
 }

The problem is the password value, does anyone know how the password it is hashed?

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

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

发布评论

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

评论(3

小嗲 2024-09-08 14:01:14

如果您在同一 IIS 服务器上有两个 ASP.NET 应用程序,则可以像这样进行 SSO。我问了这个问题并自己回答了。

此处

两个应用程序都指向您的 asp_membership 后通过将以下内容放置在 Web 配置的 system.web 部分中,

<authentication mode="Forms" />
<membership>
  <providers>
    <clear/>
    <add name="AspNetSqlMembershipProvider"
              type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
              connectionStringName="membership"
              applicationName="/"
              />
  </providers>
</membership>
<roleManager enabled="true" />

确保两者具有相同的 applicationname 属性集。

我使用的是 IIS 6,因此我将其配置为自动为这两个应用程序生成计算机密钥。由于这两个应用程序都位于同一台计算机上,因此密钥是相同的,这是使 SSO 正常工作的关键部分。设置 IIS 后,以下内容被添加到我的 web.config 中,

<machineKey decryptionKey="AutoGenerate" validation="SHA1" validationKey="AutoGenerate" />

这就是全部内容。完成后,我可以登录 app1,然后浏览到 app2 并保留我的安全凭证。

if you have two asp.net apps on the same IIS server, you can do SSO like this. I asked this question and answered it myself.

here

Once you have both apps pointing at your asp_membership database by placing the following in the system.web section of your web config

<authentication mode="Forms" />
<membership>
  <providers>
    <clear/>
    <add name="AspNetSqlMembershipProvider"
              type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
              connectionStringName="membership"
              applicationName="/"
              />
  </providers>
</membership>
<roleManager enabled="true" />

make sure both have the same applicationname property set.

I was using IIS 6 so I configured it to autogenerate a machine key for both applications. Because both of these applications live on the same machine the key would be identical, this is the critical part to making the SSO work. After setting up IIS the following was added to my web.config

<machineKey decryptionKey="AutoGenerate" validation="SHA1" validationKey="AutoGenerate" />

That was all there was to it. Once that was done I could log into app1 and then browse to app2 and keep my security credentials.

心的位置 2024-09-08 14:01:14

问题是密码值,
有谁知道密码是怎么来的
是否经过哈希处理?

是的 - 你知道!检查您的 web.config 文件是否有类似以下内容:

<membership defaultProvider="MembershipSqlProvider" 
userIsOnlineTimeWindow="15">
 <providers>
  <add name="MembershipSqlProvider" 
    type="System.Web.Security.SqlMembershipProvider, System.Web,
    Version=1.2.3400.0, Culture=neutral, 
    PublicKeyToken=b03f5f7f11d50a3a" 

    PasswordFormat="Hashed" />
 </providers>
</membership>

PasswordFormat 就是您要查找的内容。它可以具有以下三个值:

  • Clear
  • Encrypted
  • Hashed

并且,Microsoft 将默认值设置为 Hashed <代码>密码格式。

The problem is the password value,
does anyone know how the password it
is hashed?

Yes - you do! Check your web.config file for something like this:

<membership defaultProvider="MembershipSqlProvider" 
userIsOnlineTimeWindow="15">
 <providers>
  <add name="MembershipSqlProvider" 
    type="System.Web.Security.SqlMembershipProvider, System.Web,
    Version=1.2.3400.0, Culture=neutral, 
    PublicKeyToken=b03f5f7f11d50a3a" 

    PasswordFormat="Hashed" />
 </providers>
</membership>

The PasswordFormat is what you are looking for. It can have the following three values:

  • Clear
  • Encrypted
  • Hashed

And, Microsoft sets the default value to Hashed for PasswordFormat.

再见回来 2024-09-08 14:01:14

为什么不通过 System.Web.Security.Membership.ValidateUser()

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>    
        <membership defaultProvider="MyMembershipProvider">
            <providers>
                <clear />
                <add name="MyMembershipProvider" type="MyApplication.MyMembershipProvider" connectionStringName="MyConnString" />
            </providers>
        </membership>
    </system.web>
</configuration>

Why don't check it automatically via System.Web.Security.Membership.ValidateUser() ?

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>    
        <membership defaultProvider="MyMembershipProvider">
            <providers>
                <clear />
                <add name="MyMembershipProvider" type="MyApplication.MyMembershipProvider" connectionStringName="MyConnString" />
            </providers>
        </membership>
    </system.web>
</configuration>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文