C# Guid 应包含 32 位数字和 4 个破折号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11224493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:03:08  来源:igfitidea点击:

Guid should contain 32 digits with 4 dashes

c#asp.net

提问by user1467175

I had a website which contains a createuserwizard control. And upon creating an account, verification email along with its verify URL would be send to the user's email address.

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

However, when i have a test-run, upon clicking the URL in the email, this error appear:

但是,当我进行测试运行时,在单击电子邮件中的 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. 

Another funny thing is, the verification URL in my test-run look weird :

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

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

Normally, the URL should like this(which is a whole lot of characters at the end of the URL:

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

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

I was actually thinking that, it there a connection with the end of the URL with my problem of the error (Guid should contain 32 digits with 4 dashes)..

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

The code that generates the URL is as follows:

生成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); 
}

Please give me suggestions and what I should do to solve this problems.

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

Thanks in advance.

提前致谢。

UPDATE:

更新:

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);

}

Now my URL Link look like this :

现在我的 URL 链接如下所示:

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

But the error "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" still remained

但错误“Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”仍然存在

采纳答案by Jon Egerton

You've got a line that says:

你有一行写着:

Guid userInfoId = (Guid)userInfo.ProviderUserKey;

Surely this is what you should provide in the URL?

这肯定是您应该在 URL 中提供的内容吗?

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

回答by spender

You're setting the ID as:

您将 ID 设置为:

User.ToString()

This resolves to the string:

这解析为字符串:

"System.Security.Principal.GenericPrincipal"

I don't see a GUID anywhere, so it's anyone's guess as to how you want to generate this.

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

回答by Hari Gillala

You are not passing ID which is in your case GUID, You are trying to pass ID value=User.Tostring().

您没有传递 ID,在您的情况下是 GUID,您正在尝试传递 ID value=User.Tostring().

回答by Michael

You should make two changes. Firstly, as mentioned User.ToString()will always produce "System.Security.Principal.GenericPrincipal". You should change this to:

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

User.Guid.ToString()

Secondly, your web page should code more defensively and use TryParse as in:

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

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