【问题标题】:Guid should contain 32 digits with 4 dashesGuid 应包含 32 位数字和 4 个破折号
【发布时间】:2012-06-27 20:33:34
【问题描述】:

我有一个包含 createuserwizard 控件的网站。在创建帐户后,验证电子邮件及其验证 URL 将被发送到用户的电子邮件地址。

但是,当我进行测试运行时,在单击电子邮件中的 URL 时,会出现此错误:

Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).


Source Error: 

Line 17:         {
Line 18:             //store the user id
*Line 19:             Guid userId = new Guid(Request.QueryString["ID"]);*
Line 20: 
Error appeared on LINE 19. 

另一个有趣的事情是,我的测试运行中的验证 URL 看起来很奇怪:

http://localhost:4635/WebSite2/Verify.aspx?ID=System.Security.Principal.GenericPrincipal

通常,URL 应该是这样的(在 URL 的末尾有很多字符:

http://localhost:2180/LoginPage/EmailConfirmation.aspx?ID=204696d6-0255-41a7-bb0f-4d7851bf7200

我实际上是在想,它与我的错误问题(Guid 应该包含 32 位数字和 4 个破折号)与 URL 的末尾有联系。

生成URL的代码如下:

protected void CreateUserWizard1_SendingMail(object sender,MailMessageEventArgs e)
{ 
    string domainName = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath; 
    string confirmationPage = "/Verify.aspx?ID=" + User.ToString(); 
    string url = domainName + confirmationPage; 
    e.Message.Body = e.Message.Body.Replace("<%VerificationUrl%>", url); 
}

请给我一些建议以及我应该做些什么来解决这个问题。

提前致谢。

更新:

protected void CreateUserWizard1_SendingMail(object sender,MailMessageEventArgs e)
{
    MembershipUser userInfo = Membership.GetUser(CreateUserWizard1.UserName);
    Guid userInfoId = (Guid)userInfo.ProviderUserKey;

    string domainName = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath;
    string confirmationPage = "/Verify.aspx?ID=" + userInfo.ToString();
    string url = domainName + confirmationPage;

    e.Message.Body = e.Message.Body.Replace("<%VerificationUrl%>", url);

}

现在我的 URL 链接看起来像这样:

http://localhost:4635/WebSite2/Verify.aspx?ID=username

但错误“Guid 应包含 32 位数字和 4 个破折号(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”仍然存在

【问题讨论】:

  • 向我们展示生成链接的代码怎么样?这显然是问题所在。
  • 这是生成 URL 的代码: protected void CreateUserWizard1_SendingMail(object sender,MailMessageEventArgs e) { //MembershipUser userInfo = Membership.GetUser(CreateUserWizard1.UserName); //Guid userInfoId = (Guid)userInfo.ProviderUserKey;字符串域名 = Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath; string ConfirmationPage = "/Verify.aspx?ID=" + User.ToString();字符串 url = 域名 + 确认页面; e.Message.Body = e.Message.Body.Replace("", url); }

标签: c# asp.net


【解决方案1】:

你有一行说:

Guid userInfoId = (Guid)userInfo.ProviderUserKey;

这确实是您应该在 URL 中提供的内容吗?

string confirmationPage = "/Verify.aspx?ID=" + userInfoId.ToString();

【讨论】:

  • 是的,这一行是网址的后面
  • 是的,但我的版本和你的不同。我用过userInfoId.ToString()。你用过userInfo.ToString()
  • 你是对的,我把它改成userInfoId,它解决了错误!谢谢(:
【解决方案2】:

您将 ID 设置为:

User.ToString()

这会解析为字符串:

"System.Security.Principal.GenericPrincipal"

我在任何地方都没有看到 GUID,所以任何人都可以猜测您要如何生成它。

【讨论】:

  • 我应该把这个“System.Security.Principal.GenericPrincipal”放在哪里?作为命名空间?
【解决方案3】:

您没有传递您的案例 GUID 中的 ID,您正在尝试传递 ID value=User.Tostring().

【讨论】:

    【解决方案4】:

    您应该进行两项更改。首先,如前所述,User.ToString() 将始终产生"System.Security.Principal.GenericPrincipal"。您应该将其更改为:

    User.Guid.ToString()
    

    其次,您的网页应该更具防御性并使用 TryParse,如下所示:

      Guid g;
      if (Guid.TryParse(Request.QueryString["ID"], out g))
        // its a good Guid
      else
        // its not a valid Guid
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多