MembershipCreateUserException 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
当成员资格提供程序未能成功创建用户时引发的异常。
public ref class MembershipCreateUserException : Exception
[System.Serializable]
public class MembershipCreateUserException : Exception
[<System.Serializable>]
type MembershipCreateUserException = class
inherit Exception
Public Class MembershipCreateUserException
Inherits Exception
- 继承
- 属性
示例
下面的代码示例为配置为使用表单身份验证和 ASP.NET 成员身份的 ASP.NET 应用程序创建一个新用户。 如果未成功创建用户,代码将 MembershipCreateUserException 捕获并在属性中指定的 StatusCode 消息。
重要
此示例包含接受用户输入的文本框,这是潜在的安全威胁。 默认情况下,ASP.NET 网页验证用户输入是否不包含脚本或 HTML 元素。 有关详细信息,请参阅脚本侵入概述。
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
public void CreateUser_OnClick(object sender, EventArgs args)
{
MembershipCreateStatus result;
try
{
// Create new user.
if (Membership.RequiresQuestionAndAnswer)
{
MembershipUser newUser = Membership.CreateUser(
UsernameTextbox.Text,
PasswordTextbox.Text,
EmailTextbox.Text,
PasswordQuestionTextbox.Text,
PasswordAnswerTextbox.Text,
false,
out result);
}
else
{
MembershipUser newUser = Membership.CreateUser(
UsernameTextbox.Text,
PasswordTextbox.Text,
EmailTextbox.Text);
}
Response.Redirect("login.aspx");
}
catch (MembershipCreateUserException e)
{
Msg.Text = GetErrorMessage(e.StatusCode);
}
catch (HttpException e)
{
Msg.Text = e.Message;
}
}
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.";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Create User</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Create New User</h3>
<asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
<table cellpadding="3" border="0">
<tr>
<td>Username:</td>
<td><asp:Textbox id="UsernameTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
ControlToValidate="UserNameTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<tr>
<td>Password:</td>
<td><asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /></td>
<td><asp:RequiredFieldValidator id="PasswordRequiredValidator" runat="server"
ControlToValidate="PasswordTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><asp:Textbox id="PasswordConfirmTextbox" runat="server" TextMode="Password" /></td>
<td><asp:RequiredFieldValidator id="PasswordConfirmRequiredValidator" runat="server"
ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" />
<asp:CompareValidator id="PasswordConfirmCompareValidator" runat="server"
ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
Display="Static" ControlToCompare="PasswordTextBox"
ErrorMessage="Confirm password must match password." />
</td>
</tr>
<tr>
<td>Email Address:</td>
<td><asp:Textbox id="EmailTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="EmailRequiredValidator" runat="server"
ControlToValidate="EmailTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<% if (Membership.RequiresQuestionAndAnswer) { %>
<tr>
<td>Password Question:</td>
<td><asp:Textbox id="PasswordQuestionTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="PasswordQuestionRequiredValidator" runat="server"
ControlToValidate="PasswordQuestionTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<tr>
<td>Password Answer:</td>
<td><asp:Textbox id="PasswordAnswerTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="PasswordAnswerRequiredValidator" runat="server"
ControlToValidate="PasswordAnswerTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<% } %>
<tr>
<td></td>
<td><asp:Button id="CreateUserButton" Text="Create User" OnClick="CreateUser_OnClick" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Public Sub CreateUser_OnClick(sender As Object, args As EventArgs)
Dim result As MembershipCreateStatus
Try
' Create new user.
Dim newUser As MembershipUser
If Membership.RequiresQuestionAndAnswer Then
newUser = Membership.CreateUser( _
UsernameTextbox.Text, _
PasswordTextbox.Text, _
EmailTextbox.Text, _
PasswordQuestionTextbox.Text, _
PasswordAnswerTextbox.Text, _
false, _
result)
Else
newUser = Membership.CreateUser( _
UsernameTextbox.Text, _
PasswordTextbox.Text, _
EmailTextbox.Text)
End If
Response.Redirect("login.aspx")
Catch e As MembershipCreateUserException
Msg.Text = GetErrorMessage(e.StatusCode)
Catch e As HttpException
Msg.Text = e.Message
End Try
End Sub
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
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Create User</title>
</head>
<body>
<form id="form1" runat="server">
<h3>Create New User</h3>
<asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
<table cellpadding="3" border="0">
<tr>
<td>Username:</td>
<td><asp:Textbox id="UsernameTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="UsernameRequiredValidator" runat="server"
ControlToValidate="UserNameTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<tr>
<td>Password:</td>
<td><asp:Textbox id="PasswordTextbox" runat="server" TextMode="Password" /></td>
<td><asp:RequiredFieldValidator id="PasswordRequiredValidator" runat="server"
ControlToValidate="PasswordTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<tr>
<td>Confirm Password:</td>
<td><asp:Textbox id="PasswordConfirmTextbox" runat="server" TextMode="Password" /></td>
<td><asp:RequiredFieldValidator id="PasswordConfirmRequiredValidator" runat="server"
ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" />
<asp:CompareValidator id="PasswordConfirmCompareValidator" runat="server"
ControlToValidate="PasswordConfirmTextbox" ForeColor="red"
Display="Static" ControlToCompare="PasswordTextBox"
ErrorMessage="Confirm password must match password." />
</td>
</tr>
<tr>
<td>Email Address:</td>
<td><asp:Textbox id="EmailTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="EmailRequiredValidator" runat="server"
ControlToValidate="EmailTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<% If Membership.RequiresQuestionAndAnswer Then %>
<tr>
<td>Password Question:</td>
<td><asp:Textbox id="PasswordQuestionTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="PasswordQuestionRequiredValidator" runat="server"
ControlToValidate="PasswordQuestionTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<tr>
<td>Password Answer:</td>
<td><asp:Textbox id="PasswordAnswerTextbox" runat="server" /></td>
<td><asp:RequiredFieldValidator id="PasswordAnswerRequiredValidator" runat="server"
ControlToValidate="PasswordAnswerTextbox" ForeColor="red"
Display="Static" ErrorMessage="Required" /></td>
</tr>
<% End If %>
<tr>
<td></td>
<td><asp:Button id="CreateUserButton" Text="Create User" OnClick="CreateUser_OnClick" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>
注解
操作失败时CreateUser会引发类的MembershipCreateUserException实例。
备注
如果不熟悉 ASP.NET 的成员身份功能,请参阅“成员身份简介”,然后再继续操作。 有关与成员身份相关的其他主题的列表,请参阅 使用成员身份管理用户。
该 StatusCode 属性指示异常的原因,以便可以适当地处理它。
构造函数
MembershipCreateUserException() |
初始化 MembershipCreateUserException 类的新实例。 |
MembershipCreateUserException(MembershipCreateStatus) |
使用指定的 StatusCode 值初始化 MembershipCreateUserException 类的新实例。 |
MembershipCreateUserException(SerializationInfo, StreamingContext) |
用提供的序列化信息和上下文初始化 MembershipCreateUserException 类的新实例。 |
MembershipCreateUserException(String) |
初始化 MembershipCreateUserException 类的新实例,并将 Message 属性设置为所提供的 |
MembershipCreateUserException(String, Exception) |
初始化 MembershipCreateUserException 类的新实例,并将 Message 属性设置为提供的 |
属性
Data |
获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。 (继承自 Exception) |
HelpLink |
获取或设置指向与此异常关联的帮助文件链接。 (继承自 Exception) |
HResult |
获取或设置 HRESULT(一个分配给特定异常的编码数字值)。 (继承自 Exception) |
InnerException |
获取导致当前异常的 Exception 实例。 (继承自 Exception) |
Message |
获取描述当前异常的消息。 (继承自 Exception) |
Source |
获取或设置导致错误的应用程序或对象的名称。 (继承自 Exception) |
StackTrace |
获取调用堆栈上的即时框架字符串表示形式。 (继承自 Exception) |
StatusCode |
获取对异常原因的描述。 |
TargetSite |
获取引发当前异常的方法。 (继承自 Exception) |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetBaseException() |
当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。 (继承自 Exception) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetObjectData(SerializationInfo, StreamingContext) |
使用将目标对象序列化所需的数据填充 SerializationInfo。 |
GetType() |
获取当前实例的运行时类型。 (继承自 Exception) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
创建并返回当前异常的字符串表示形式。 (继承自 Exception) |
事件
SerializeObjectState |
已过时。
当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。 (继承自 Exception) |