Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Adding Roles to ASP.NET Accounts
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Adding Roles to ASP.NET Accounts
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 11
    2006-11-22

    Table of Contents:
  • Adding Roles to ASP.NET Accounts
  • Restricting Access to Pages Based on Roles
  • Create Personalized Web Sites
  • Exploring the Pro

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Adding Roles to ASP.NET Accounts - Restricting Access to Pages Based on Roles


    (Page 2 of 4 )

    There are two ways to restrict access to a page based on membership in a Role. The first is to test if the logged-in user is in a particular role, using the User.IsInRole() method:

      Dim isManager as boolean = User.IsInRole("Manager")

    You might redirect the user to an error page if the user is not in the required role. As an example, let’s add code that blocks nonmanagers from linking to the Manage Roles page. To do so, add a test in thePage_Loadmethod of ManageRoles.aspx.vb:

      Protected Sub Page_Load(_
      ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles Me.Load
         
    If User.IsInRole("Manager") = False Then
             Response.Redirect("NoPrivs.aspx")
         End If

    If the user is not in the role of “Manager,” the user is redirected to the page NoPrivs.aspx. That page can display an error message and then allow the user to take other actions. A very simple example is shown in Figure 12-29.


    Figure 12-29.  NoPrivs.aspx

    The code for the button (btnHome) on the NoPrivs.aspx.vb page, whose text is “Return to Welcome,” is very simple and shown in Example 12-9.

    Example 12-9. Return to Welcome button Click event handler

    .Protected Sub btnHome_Click(_
    ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles btnHome.Click
       
    Response.Redirect("Welcome.aspx")
    End Sub

    Restricting Access to a Set of Pages

    You can also restrict access to a set of pages by adding an authorization section to a Web.config file. You place this file in a subdirectory to control access to all files in that subdirectory and all of its subdirectories, and you use the location element to control access to specific files:

      <authorization>
        <deny users='?' />
        <allow roles='Manager' />
        <deny users='*' />
     
    </authorization>

    The first line (deny users='?') prohibits access to anyone who is not logged in. The second line (allow roles='Manager') allows access to anyone in the Manager role, and the final line (deny users='*') disallows everyone, but is overridden by theallow roles.

    More Visual Basic.NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming Visual Basic 2005," published...
       · This code has helped a lot! Only thing is I'm getting an error that some of the...
       · I read this article because I was looking for a way to add membership to my website....
       · This excerpted article should explain the term "WAT." I don't know WAT that means. ...
     

    Buy this book now. This article is excerpted from chapter 12 of the book Programming Visual Basic 2005, written by Jesse Liberty (O'Reilly, 2005; ISBN: 0596009496). Check it out today at your favorite bookstore. Buy this book now.

    VISUAL BASIC.NET ARTICLES

    - Clocks and Countdowns
    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek