简体   繁体   English

ThreadAbort使用ASP.NET身份模型

[英]ThreadAbort on Using ASP.NET Identity Model

I was following this post on email confirmation except for ASP.NET. 除了ASP.NET之外,我在电子邮件确认上关注这篇文章 The following code is causing a 以下代码导致a

Exception Details: System.Threading.ThreadAbortException: Thread was being aborted.

The page is marked Async: 该页面标记为异步:

<%@ Page Title="Register" Async="true" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="AlphaFrontEndSiteASP.Account.Register" %>

The code is: 代码是:

public partial class Register : Page
    {
        protected async void CreateUser_Click(object sender, EventArgs e)
        {
            try
            {
                string confirmationToken = CreateConfirmationToken();
                var user = new ApplicationUser
                {
                    UserName = UserName.Text,
                    CompanyName = CompanyName.Text,
                    emailAddress = emailAddress.Text,
                    FirstName = FirstName.Text,
                    LastName = LastName.Text,
                    ConfirmationToken = confirmationToken,
                    IsConfirmed = false
                };

                var manager = new UserManager();
                var result = await manager.CreateAsync(user, Password.Text);
                if (result.Succeeded)
                {
                    SendEmailConfirmation(emailAddress.Text, UserName.Text, confirmationToken);
                }
                Response.Redirect("~/Default.aspx");
            }
            catch (Exception ex)
            {
                common c = new common();
                c.LogError(ex.Message, "Register.aspx.cs" + " - " + this.GetType().FullName + "." + System.Reflection.MethodBase.GetCurrentMethod().Name);
            }

 private string CreateConfirmationToken()
        {
            return Guid.NewGuid().ToString();
        }

        private void SendEmailConfirmation(string to, string username, string confirmationToken)
        {
            common c = new common();
            c.SendMail("foo@foo.net", to, username, confirmationToken + Environment.NewLine + "http://blah.com/Activate.aspx?confirmationCode= " + confirmationToken);
        }

Does anyone have any ideas why? 有没有人有任何想法?

By design - Response.Redirect stops request processing by aborting the thread: 按设计 - Response.Redirect通过中止线程来停止请求处理:

Redirect calls End which throws a ThreadAbortException exception upon completion. 重定向调用End,在完成时抛出ThreadAbortException异常。

  ....
  Response.Redirect("~/Default.aspx");
 }
 catch (Exception ex)

Depending on you other processing you can either 根据您的其他处理,您也可以

  • specify false as second argument of other Redirect override false指定为其他重定向覆盖的第二个参数
  • ignore/no log ThreadAbortException in that piece of code 忽略/不记录该段代码中的ThreadAbortException
  • redesign code to do redirect differently. 重新设计代码以不同方式重定向。

Note: if you use ASP.Net MVC it's Controller.Redirect does not throw exception. 注意:如果你使用ASP.Net MVC,它的Controller.Redirect不会抛出异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM