用户无法使用Asp.Net验证帐户

时间:2012-01-17 10:20:02

标签: asp.net email activation createuserwizard

我有一个简单的创建用户向导和自定义成员资格提供程序,它取自here

现在我正在通过scott Mitchell关注本教程并使用向导创建新用户并能够通过将Disable create property user设置为“False”来发送电子邮件,以便每当用户收到激活链接时,他需要单击该验证他的帐户。

现在的问题是,当他创建新用户时,它工作正常,当他尝试立即登录时,他会收到消息,他需要首先激活链接才能登录。

注册后,他收到电子邮件,当他点击链接时,它会给我一个错误,即数据库中没有用户。

如下所示,用户获得激活链接 enter image description here

当用户试图点击它时,他得知他在数据库中找不到 enter image description here

如果我签入管理工具,如果我检查用户是否可用,旁边没有勾选。 enter image description here

这是我的web.config:

    <?xml version="1.0"?>

<configuration>
  <connectionStrings>
    <add name="HDIConnectionString"
     connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|HDIMembershipProvider.mdf"/>
  </connectionStrings>
  <system.web>
    <roleManager defaultProvider="CustomProvider">
      <providers>
        <add connectionStringName="HDIConnectionString" name="CustomProvider"
          type="System.Web.Security.SqlRoleProvider" />
      </providers>
    </roleManager>
    <membership defaultProvider="HDIMembershipProvider">
      <providers>
        <clear/>
        <add name="HDIMembershipProvider" type="HDI.AspNet.Membership.HDIMembershipProvider" connectionStringName="HDIConnectionString" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Clear"/>
      </providers>
    </membership>
    <machineKey validationKey="C50B3C89CB21F4F1422FF158A5B42D0E8DB8CB5CDA1742572A487D9401E3400267682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE" decryptionKey="8A9BE8FD67AF6979E7D20198CFEA50DD3D3799C77AF2B72F" validation="SHA1"/>
    <authentication mode="Forms">
      <forms name=".ASPXFORMSAUTH" loginUrl="Login.aspx" />
    </authentication>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
  </system.web>
  <appSettings>
    <add key="adminEmail" value="noreply@xyz.com"/>
  </appSettings>
  <system.net>
    <mailSettings>
      <smtp from="xyz@gmail.com">
        <network host="smtp.gmail.com" password="password" port="587" userName="xyz@gmail.com"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

createuser.aspx的代码:

 Protected Sub CreateUserWizard1_SendingMail(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MailMessageEventArgs) Handles CreateUserWizard1.SendingMail
        Dim userInfo As MembershipUser = Membership.GetUser(CreateUserWizard1.UserName)

        'Construct the verification URL
        Dim verifyUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) & Page.ResolveUrl("~/Verify.aspx?ID=" & userInfo.ProviderUserKey.ToString())

        'Replace <%VerifyUrl%> placeholder with verifyUrl value
        e.Message.Body = e.Message.Body.Replace("<%VerifyUrl%>", verifyUrl)
    End Sub

验证Page_Load:

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Make sure that a valid querystring value was passed through
        If String.IsNullOrEmpty(Request.QueryString("ID")) OrElse Not Regex.IsMatch(Request.QueryString("ID"), "[0-9a-f]{8}\-([0-9a-f]{4}\-){3}[0-9a-f]{12}") Then
            InformationLabel.Text = "An invalid ID value was passed in through the querystring."
        Else
            'ID exists and is kosher, see if this user is already approved
            'Get the ID sent in the querystring
            Dim userId As Guid = New Guid(Request.QueryString("ID"))

            'Get information about the user
            Dim userInfo As MembershipUser = Membership.GetUser(userId)
            If userInfo Is Nothing Then
                'Could not find user!
                InformationLabel.Text = "The user account could not be found in the membership database."
            Else
                'User is valid, approve them
                userInfo.IsApproved = True
                Membership.UpdateUser(userInfo)

                'Display a message
                InformationLabel.Text = "Your account has been verified and you can now log into the site."
            End If
        End If

这是数据库截图: enter image description here

2 个答案:

答案 0 :(得分:0)

您正在将GUID类型传递给Membership类的GetUser方法。

更新现在已经测试过了。传递GUID会调用正确的重载 - GetUser(object providerUserKey)。所以这个答案是不相关的。

如何确保在运行时将其推断为正确的重载?

GetUser has both string and object single parameter overloads,因此将guid作为对象传递是有意义的,因此您明确说明要调用哪个重载。

框架可能正在你的guid上调用ToString(),这将调用查找用户名而不是提供者密钥的重载。

现在不在电脑上,但它应该是这样的......

      Dim key as new object()
'put the guid in the object type
    key = Userid
    Dim user = Membership.GetUser(key)

答案 1 :(得分:0)

@Tim和Baldy-我终于开始工作,但没有使用UserID。我不知道GUID有什么问题,我尝试使用用户名,它运行得很好。

因此,如果使用GUID进行任何修改,请告诉我。