SqlRoleProvider.RoleExists(String) 메서드

정의

지정된 역할 이름이 역할 데이터베이스에 이미 있는지 여부를 나타내는 값을 가져옵니다.

public:
 override bool RoleExists(System::String ^ roleName);
public override bool RoleExists (string roleName);
override this.RoleExists : string -> bool
Public Overrides Function RoleExists (roleName As String) As Boolean

매개 변수

roleName
String

데이터베이스에서 검색할 역할 이름입니다.

반환

Boolean

역할 이름이 데이터베이스에 이미 있으면 true이고, 그렇지 않으면 false입니다.

예외

roleName이(가) null인 경우

roleName이 빈 문자열이거나 쉼표를 포함하는 경우

또는

roleName이 256자보다 긴 경우

데이터베이스와 통신하는 동안 알 수 없는 오류가 발생한 경우

예제

다음 코드 예제에서는 RoleExists 역할 이름이 역할을 만들기 전에 이미 있는지 여부를 결정 하는 방법입니다. 역할 관리를 사용 하도록 설정 하는 Web.config 파일의 예제를 참조 하세요. SqlRoleProvider합니다.

중요

이 예제에서는 잠재적 보안 위협을 사용자 입력을 허용 하는 텍스트 상자가 포함 되어 있습니다. 기본적으로 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">

string[] rolesArray;

public void Page_Load(object sender, EventArgs args)
{
  if (!IsPostBack)
  {
    // Bind roles to GridView.

    rolesArray = Roles.GetAllRoles();
    RolesGrid.DataSource = rolesArray;
    RolesGrid.DataBind();
  }
}

public void CreateRole_OnClick(object sender, EventArgs args)
{
  string createRole = RoleTextBox.Text;

  try
  {
    if (Roles.RoleExists(createRole))
    {
      Msg.Text = "Role '" + Server.HtmlEncode(createRole) + "' already exists. Please specify a different role name.";
      return;
    }

    Roles.CreateRole(createRole);

    Msg.Text = "Role '" + Server.HtmlEncode(createRole) + "' created.";

    // Re-bind roles to GridView.

    rolesArray = Roles.GetAllRoles();
    RolesGrid.DataSource = rolesArray;
    RolesGrid.DataBind();
  }
  catch (Exception e)
  {
    Msg.Text = "Role '" + Server.HtmlEncode(createRole) + "' <u>not</u> created.";
    Response.Write(e.ToString());
  }

}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Create Role</title>
</head>
<body>

<form runat="server" id="PageForm">
  <h3>Create a Role</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  Role name: 

  <asp:TextBox id="RoleTextBox" runat="server" />

  <asp:Button Text="Create Role" id="CreateRoleButton"
              runat="server" OnClick="CreateRole_OnClick" />

  <br />

  <asp:GridView runat="server" CellPadding="2" id="RolesGrid" 
                Gridlines="Both" CellSpacing="2" AutoGenerateColumns="false" >
    <HeaderStyle BackColor="navy" ForeColor="white" />
    <Columns>
      <asp:TemplateField HeaderText="Roles" >
        <ItemTemplate>
          <%# Container.DataItem.ToString() %>
        </ItemTemplate>
      </asp:TemplateField>
    </Columns>
   </asp:GridView>
</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">

Dim rolesArray() As String

Public Sub Page_Load(sender As Object, args As EventArgs)

  If Not IsPostBack Then
    ' Bind roles to GridView.

    rolesArray = Roles.GetAllRoles()
    RolesGrid.DataSource = rolesArray
    RolesGrid.DataBind()
  End If

End Sub

Public Sub CreateRole_OnClick(sender As Object, args As EventArgs)

  Dim createRole As String = RoleTextBox.Text

  Try
    If Roles.RoleExists(createRole) Then
      Msg.Text = "Role '" & Server.HtmlEncode(createRole) & "' already exists. Please specify a different role name."
      Return
    End If

    Roles.CreateRole(createRole)

    Msg.Text = "Role '" & Server.HtmlEncode(createRole) & "' created."

    ' Re-bind roles to GridView.

    rolesArray = Roles.GetAllRoles()
    RolesGrid.DataSource = rolesArray
    RolesGrid.DataBind()
  Catch
    Msg.Text = "Role '" & Server.HtmlEncode(createRole) & "' <u>not</u> created."
  End Try

End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: Create Role</title>
</head>
<body>

<form runat="server" id="PageForm">
  <h3>Create a Role</h3>

  <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />

  Role name: 

  <asp:TextBox id="RoleTextBox" runat="server" />

  <asp:Button Text="Create Role" id="CreateRoleButton"
              runat="server" OnClick="CreateRole_OnClick" />

  <br />

  <asp:GridView runat="server" CellPadding="2" id="RolesGrid" 
                Gridlines="Both" CellSpacing="2" AutoGenerateColumns="false" >
    <HeaderStyle BackColor="navy" ForeColor="white" />
    <Columns>
      <asp:TemplateField HeaderText="Roles" >
        <ItemTemplate>
          <%# Container.DataItem.ToString() %>
        </ItemTemplate>
      </asp:TemplateField>
    </Columns>
   </asp:GridView>
</form>

</body>
</html>

설명

합니다 RoleExists 메서드를 호출 합니다 Roles 역할 이름을 ASP.NET 애플리케이션의 구성 파일 (Web.config)에 지정 된 SQL Server 데이터베이스에 있는지 여부를 결정 하는 클래스입니다.

적용 대상

추가 정보