MembershipCreateUserException 构造函数

定义

初始化 MembershipCreateUserException 类的新实例。

重载

MembershipCreateUserException()

初始化 MembershipCreateUserException 类的新实例。

MembershipCreateUserException(String)

初始化 MembershipCreateUserException 类的新实例,并将 Message 属性设置为所提供的 message 参数值。

MembershipCreateUserException(MembershipCreateStatus)

使用指定的 StatusCode 值初始化 MembershipCreateUserException 类的新实例。

MembershipCreateUserException(SerializationInfo, StreamingContext)

用提供的序列化信息和上下文初始化 MembershipCreateUserException 类的新实例。

MembershipCreateUserException(String, Exception)

初始化 MembershipCreateUserException 类的新实例,并将 Message 属性设置为提供的 message,将 InnerException 属性设置为提供的 innerException

MembershipCreateUserException()

初始化 MembershipCreateUserException 类的新实例。

public:
 MembershipCreateUserException();
public MembershipCreateUserException ();
Public Sub New ()

示例

下面的代码示例调用 Membership.CreateUser 方法以创建新的成员资格用户。 如果用户创建失败, MembershipCreateUserException 将引发 。

public MembershipUser MyCreateUser(string username, string password, string email,
                                   string question, string answer)
{
  MembershipCreateStatus status;

  MembershipUser u = Membership.CreateUser(username, password, email, question, 
                                           answer, true, out status);
  if (u == null)
  {
    throw new MembershipCreateUserException();
  }

  return u;
}
Public Function MyCreateUser(username As String, password As String, email As String, _
                             question As String, answer As String) As MembershipUser

  Dim status As MembershipCreateStatus

  Dim u As MembershipUser = Membership.CreateUser(username, password, email, question, _
                                                  answer, True, status)
  If u Is Nothing Then
    Throw New MembershipCreateUserException()
  End If

  Return u
End Function

注解

未创建用户时, MembershipCreateUserExceptionMembership.CreateUser 方法将引发 类的实例。

另请参阅

适用于

MembershipCreateUserException(String)

初始化 MembershipCreateUserException 类的新实例,并将 Message 属性设置为所提供的 message 参数值。

public:
 MembershipCreateUserException(System::String ^ message);
public MembershipCreateUserException (string message);
new System.Web.Security.MembershipCreateUserException : string -> System.Web.Security.MembershipCreateUserException
Public Sub New (message As String)

参数

message
String

对异常原因的描述。

示例

下面的代码示例调用 Membership.CreateUser 方法以创建新的成员资格用户。 如果用户创建失败,MembershipCreateUserException则会根据 方法返回CreateUser的 引发消息StatusCode

public MembershipUser MyCreateUser(string username, string password, string email,
                                   string question, string answer)
{
  MembershipCreateStatus status;

  MembershipUser u = Membership.CreateUser(username, password, email, question, 
                                           answer, true, out status);
  if (u == null)
  {
    throw new MembershipCreateUserException(GetErrorMessage(status));
  }

  return u;
}

public string GetErrorMessage(MembershipCreateStatus status)
{
   switch (status)
   {
      case MembershipCreateStatus.DuplicateUserName:
        return "Username already exists. Please enter a different user name.";

      case MembershipCreateStatus.DuplicateEmail:
        return "A username for that email address already exists. Please enter a different email address.";

      case MembershipCreateStatus.InvalidPassword:
        return "The password provided is invalid. Please enter a valid password value.";

      case MembershipCreateStatus.InvalidEmail:
        return "The email address provided is invalid. Please check the value and try again.";

      case MembershipCreateStatus.InvalidAnswer:
        return "The password retrieval answer provided is invalid. Please check the value and try again.";

      case MembershipCreateStatus.InvalidQuestion:
        return "The password retrieval question provided is invalid. Please check the value and try again.";

      case MembershipCreateStatus.InvalidUserName:
        return "The user name provided is invalid. Please check the value and try again.";

      case MembershipCreateStatus.ProviderError:
        return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

      case MembershipCreateStatus.UserRejected:
        return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

      default:
        return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
   }
}
Public Function MyCreateUser(username As String, password As String, email As String, _
                             question As String, answer As String) As MembershipUser

  Dim status As MembershipCreateStatus

  Dim u As MembershipUser = Membership.CreateUser(username, password, email, question, _
                                                  answer, True, status)
  If u Is Nothing Then
    Throw New MembershipCreateUserException(GetErrorMessage(status))
  End If

  Return u
End Function


Public Function GetErrorMessage(status As MembershipCreateStatus) As String

   Select Case status
      Case MembershipCreateStatus.DuplicateUserName
        Return "Username already exists. Please enter a different user name."

      Case MembershipCreateStatus.DuplicateEmail
        Return "A username for that email address already exists. Please enter a different email address."

      Case MembershipCreateStatus.InvalidPassword
        Return "The password provided is invalid. Please enter a valid password value."

      Case MembershipCreateStatus.InvalidEmail
        Return "The email address provided is invalid. Please check the value and try again."

      Case MembershipCreateStatus.InvalidAnswer
        Return "The password retrieval answer provided is invalid. Please check the value and try again."

      Case MembershipCreateStatus.InvalidQuestion
        Return "The password retrieval question provided is invalid. Please check the value and try again."

      Case MembershipCreateStatus.InvalidUserName
        Return "The user name provided is invalid. Please check the value and try again."

      Case MembershipCreateStatus.ProviderError
        Return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator."

      Case MembershipCreateStatus.UserRejected
        Return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator."

      Case Else
        Return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator."
   End Select
End Function

注解

MembershipCreateUserException类在未创建用户时由 Membership.CreateUser 方法引发。

可以使用 message 参数将异常的 属性设置为 Message 异常原因的有意义说明。

另请参阅

适用于

MembershipCreateUserException(MembershipCreateStatus)

使用指定的 StatusCode 值初始化 MembershipCreateUserException 类的新实例。

public:
 MembershipCreateUserException(System::Web::Security::MembershipCreateStatus statusCode);
public MembershipCreateUserException (System.Web.Security.MembershipCreateStatus statusCode);
new System.Web.Security.MembershipCreateUserException : System.Web.Security.MembershipCreateStatus -> System.Web.Security.MembershipCreateUserException
Public Sub New (statusCode As MembershipCreateStatus)

参数

statusCode
MembershipCreateStatus

一个 MembershipCreateStatus 枚举值,该值描述异常的原因。

示例

下面的代码示例调用 CreateUser 方法以创建新的成员资格用户。 如果用户创建失败, MembershipCreateUserException 则会引发 ,方法 StatusCode 返回的 CreateUser 将引发 。

public MembershipUser MyCreateUser(string username, string password, string email,
                                   string question, string answer)
{
  MembershipCreateStatus status;

  MembershipUser u = Membership.CreateUser(username, password, email, question, 
                                           answer, true, out status);

  if (u == null)
  {
    throw new MembershipCreateUserException(status);
  }

  return u;
}
Public Function MyCreateUser(username As String, password As String, email As String, _
                             question As String, answer As String) As MembershipUser

  Dim status As MembershipCreateStatus

  Dim u As MembershipUser = Membership.CreateUser(username, password, email, question, _
                                                  answer, True, status)
  If u Is Nothing Then
    Throw New MembershipCreateUserException(status)
  End If

  Return u
End Function

注解

MembershipCreateUserException类在未创建用户时由 Membership.CreateUser 方法引发。

参数 statusCode 可用于指示引发 的原因 MembershipCreateUserException 。 参数 statusCode 值由 StatusCode 属性公开。

另请参阅

适用于

MembershipCreateUserException(SerializationInfo, StreamingContext)

用提供的序列化信息和上下文初始化 MembershipCreateUserException 类的新实例。

protected:
 MembershipCreateUserException(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected MembershipCreateUserException (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new System.Web.Security.MembershipCreateUserException : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Web.Security.MembershipCreateUserException
Protected Sub New (info As SerializationInfo, context As StreamingContext)

参数

info
SerializationInfo

包含有关所引发异常的序列化对象数据的 SerializationInfo

context
StreamingContext

StreamingContext,它包含关于源或目标的上下文信息。

示例

下面的代码示例演示 类的 MembershipCreateUserException 实现,该类使用提供的序列化信息和上下文调用基类的受保护构造函数。

using System.Web.Security;
using System.Runtime.Serialization;

public sealed class MyCreateUserException : MembershipCreateUserException
{
  public MyCreateUserException(SerializationInfo info, StreamingContext context) : base(info, context)
  {
  }
}
Imports System.Web.Security
Imports System.Runtime.Serialization

Public NotInheritable Class MyCreateUserException
  Inherits MembershipCreateUserException

  Public Sub New (info As SerializationInfo, context As StreamingContext)
    MyBase.New(info, context)
  End Sub
End Class

注解

在反序列化期间调用此构造函数以重新构造在流上传输的异常对象。 有关详细信息,请参阅 XML 和 SOAP 序列化

另请参阅

适用于

MembershipCreateUserException(String, Exception)

初始化 MembershipCreateUserException 类的新实例,并将 Message 属性设置为提供的 message,将 InnerException 属性设置为提供的 innerException

public:
 MembershipCreateUserException(System::String ^ message, Exception ^ innerException);
public MembershipCreateUserException (string message, Exception innerException);
new System.Web.Security.MembershipCreateUserException : string * Exception -> System.Web.Security.MembershipCreateUserException
Public Sub New (message As String, innerException As Exception)

参数

message
String

对异常原因的描述。

innerException
Exception

导致 MembershipCreateUserException 的异常。

示例

下面的代码示例调用 CreateUser 方法以创建新的成员资格用户。 如果用户创建失败, MembershipCreateUserException 将引发 。 如果异常不是 , MembershipCreateUserException则捕获的异常作为 InnerException 引发的 的 MembershipCreateUserException 提供。

public MembershipUser MyCreateUser(string username, string password, string email)
{
  MembershipUser u = null;

  try
  {
    u = Membership.CreateUser(username, password, email);
  }
  catch (MembershipCreateUserException e)
  {  
    throw e;
  }
  catch (Exception e)
  {  
    throw new MembershipCreateUserException("An exception occurred creating the user.", e);
  }

  return u;
}
Public Function MyCreateUser(username As String, password As String, email As String) As MembershipUser

  Dim u As MembershipUser = Nothing

  Try
    u = Membership.CreateUser(username, password, email)
  Catch e As MembershipCreateUserException
    Throw e
  Catch e As Exception  
    Throw New MembershipCreateUserException("An exception occurred creating the user.", e)
  End Try

  Return u
End Function

注解

未创建用户时, MembershipCreateUserExceptionMembership.CreateUser 方法将引发 类的实例。

可以使用构造函数的 MembershipCreateUserException 此重载来提供有关创建用户时发生的已捕获异常的信息。

另请参阅

适用于