Membership.GeneratePassword メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Membership.GeneratePassword メソッドの意味・解説 

Membership.GeneratePassword メソッド

メモ : このメソッドは、.NET Framework version 2.0 で新しく追加されたものです。

指定した長さのランダム パスワードを生成します。

名前空間: System.Web.Security
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public Shared Function GeneratePassword
 ( _
    length As Integer, _
    numberOfNonAlphanumericCharacters As Integer
 _
) As String
Dim length As Integer
Dim numberOfNonAlphanumericCharacters As Integer
Dim returnValue As String

returnValue = Membership.GeneratePassword(length, numberOfNonAlphanumericCharacters)
public static string GeneratePassword
 (
    int length,
    int numberOfNonAlphanumericCharacters
)
public:
static String^ GeneratePassword (
    int length, 
    int numberOfNonAlphanumericCharacters
)
public static String GeneratePassword (
    int length, 
    int numberOfNonAlphanumericCharacters
)
public static function GeneratePassword
 (
    length : int, 
    numberOfNonAlphanumericCharacters : int
) : String

パラメータ

length

生成されるパスワードの文字数。長さは 1 ~ 128 文字である必要があります。

numberOfNonAlphanumericCharacters

生成されるパスワード内の区切り記号の文字数。

戻り値
指定した長さのランダム パスワード。

例外例外
例外の種類条件

ArgumentException

length が 1 未満か、128 よりも大きい値です。

numberOfNonAlphanumericCharacters が 0 未満か、または length を超えています。

解説解説

GeneratePassword メソッドは、ランダム パスワードを生成するのに使用され、一般的には、ユーザーのパスワードを新しい一時的なパスワードにリセットするためにメンバシップ プロバイダが実装する ResetPassword メソッドによって使用されます。

生成されるパスワードには、英数字と !@#$%^&*()_-+=[{]};:<>|./? の区切り記号のみ使用されます。生成されるパスワードには、表示されない、または印刷されない制御文字は使用されません。

メモメモ

GeneratePassword メソッドにより生成されたランダム パスワードは、PasswordStrengthRegularExpression プロパティの正規表現に合格するとは限りません。ただし、ランダム パスワードは、MinRequiredPasswordLength プロパティおよび numberOfNonAlphanumericCharacters パラメータで設定された基準は満たしています。

使用例使用例

新しいメンバシップ ユーザーを作成し、GeneratePassword メソッドを使用して新しいユーザーにランダムなパスワードを設定し、これをユーザーに表示するコード例を次に示します。

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security"
 %>
<script runat="server">

Public Sub CreateUser_OnClick(sender As
 Object, args As EventArgs)
  ' Create a new 12-character password with 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 e-mail
 address already exists. Please enter a different e-mail address."

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

      Case MembershipCreateStatus.InvalidEmail
        Return "The e-mail 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>
<head>
<title>Create User</title>
</head>
<body>

<form 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="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<script runat="server">

public void CreateUser_OnClick(object sender,
 EventArgs args)
{
  // Generate a new 12-character password with 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 e-mail
 address already exists. Please enter a different e-mail address.";

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

      case MembershipCreateStatus.InvalidEmail:
        return "The e-mail 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>
<head>
<title>Create User</title>
</head>
<body>

<form 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>
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

Membership.GeneratePassword メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Membership.GeneratePassword メソッドのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS