Membership.GeneratePassword(Int32, Int32) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
지정한 길이의 임의의 암호를 생성합니다.
public:
static System::String ^ GeneratePassword(int length, int numberOfNonAlphanumericCharacters);
public static string GeneratePassword (int length, int numberOfNonAlphanumericCharacters);
static member GeneratePassword : int * int -> string
Public Shared Function GeneratePassword (length As Integer, numberOfNonAlphanumericCharacters As Integer) As String
매개 변수
- length
- Int32
생성된 암호의 문자 수입니다. 암호 길이는 1에서 128자 사이여야 합니다.
- numberOfNonAlphanumericCharacters
- Int32
생성된 암호에서 영숫자 이외의 문자(예: @, #, !, %, & 등)에 대한 최소 수입니다.
반환
지정한 길이의 임의의 암호입니다.
예외
length
가 1보다 작거나 128보다 큰 경우 -또는-
numberOfNonAlphanumericCharacters
가 0보다 작거나 length
보다 큽니다.
예제
다음 코드 예제에서는 새 멤버 자격 사용자를 만들고 메서드를 사용하여 GeneratePassword 새 사용자의 암호를 임의의 값으로 초기화합니다. 이 값은 사용자에게 표시됩니다.
중요
이 예제에서는 잠재적 보안 위협을 사용자 입력을 허용 하는 텍스트 상자가 포함 되어 있습니다. 기본적으로 ASP.NET 웹 페이지는 사용자 입력 내용에 스크립트 또는 HTML 요소가 포함되어 있지 않은지 확인합니다. 자세한 내용은 Script Exploits Overview를 참조하세요.
<%@ 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)
{
// Generate a new 12-character password with at least 1 non-alphanumeric character.
string password = Membership.GeneratePassword(12, 1);
try
{
// Create new user.
MembershipUser newUser = Membership.CreateUser(UsernameTextbox.Text, password,
EmailTextbox.Text);
Msg.Text = "User <b>" + Server.HtmlEncode(UsernameTextbox.Text) + "</b> created. " +
"Your temporary password is " + password + ".";
}
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.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>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>
<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)
' Generate a new 12-character password with at least 1 non-alphanumeric character.
Dim password As String = Membership.GeneratePassword(12, 1)
Try
' Create new user.
Dim newUser As MembershipUser = Membership.CreateUser(UsernameTextbox.Text, password, _
EmailTextbox.Text)
Msg.Text = "User <b>" & Server.HtmlEncode(UsernameTextbox.Text) & "</b> created. " & _
"Your temporary password is " & password & "."
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.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>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>
<tr>
<td></td>
<td><asp:Button id="CreateUserButton" Text="Create User" OnClick="CreateUser_OnClick" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>
설명
메서드는 GeneratePassword 임의 암호를 생성하는 데 사용되며 멤버 자격 공급자가 구현한 메서드에서 사용자의 암호를 새 임시 암호로 다시 설정하는 데 가장 일반적으로 사용됩니다 ResetPassword .
생성된 암호에는 영숫자 문자만 포함되며 문장 부호는 !@#$%^&*()_-+=[{]};:<>|./?
입니다. 생성된 암호에는 숨겨지거나 인쇄할 수 없는 컨트롤 문자가 포함되지 않습니다.
참고
메서드에서 만든 임의의 암호는 속성의 GeneratePassword 정규식을 PasswordStrengthRegularExpression 전달하도록 보장되지 않습니다. 그러나 임의 암호는 속성 및 매개 변수에 MinRequiredPasswordLength 의해 설정된 조건을 충족합니다 numberOfNonAlphanumericCharacters
.
적용 대상
추가 정보
.NET