2012-01-17 58 views
2

我有一個簡單的創建這是從here用戶無法使用Asp.Net

採取現在我通過scott Mitchell下面這個教程和使用嚮導創建新的用戶,並能夠發送用戶嚮導和自定義成員資格提供程序來驗證帳戶通過設置禁用創建屬性用戶爲「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="[email protected]"/> 
    </appSettings> 
    <system.net> 
    <mailSettings> 
     <smtp from="[email protected]"> 
     <network host="smtp.gmail.com" password="password" port="587" userName="[email protected]"/> 
     </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

+0

verify.aspx中的代碼怎麼樣 - 你可以發佈它嗎? – Baldy 2012-01-17 10:24:07

+0

@ Baldy-I已經更新了它。對此,請執行此操作:) – coder 2012-01-17 10:24:40

+0

數據庫中是否存在具有來自URL-Parameter的GUID的用戶? – 2012-01-17 10:28:27

回答

0

@Tim和禿子,我終於得到了工作,但不能與UserID.I不知道這有什麼錯GUID和我用它的用戶名嘗試它,它的工作完美。

因此,如果對GUID進行任何修改,請告訴我。

0

您正在將GUID類型傳遞給Membership類的GetUser方法。

UPDATE現在已經測試過了。傳遞一個GUID確實會調用正確的超載 - GetUser(object providerUserKey)。所以這個答案是不相關的。

如何確定在運行時推斷正確的過載?

GetUser has both string and object single parameter overloads,因此將guid作爲對象傳遞是有意義的,因此您明確指出要調用哪個超載。

框架可能會在您的guid上調用ToString(),這會調用查找用戶名而不是提供者鍵的重載。

在計算機

現在不行,但它應該是這樣的......

​​
+0

@ Baldy-So我該如何改變這個問題,你能爲此發佈一些代碼嗎? – coder 2012-01-17 10:37:15

+0

@Baldy:他正在調用需要一個對象的重載,因爲他已經傳遞了一個GUID。如果它是一個字符串或布爾值,則會調用GetUser的其他重載版本。 – 2012-01-17 10:41:00

+0

爲你增加了一個例子 – Baldy 2012-01-17 10:41:49