Authentication by Roles

Last post 07-16-2009 1:41 PM by saurabh29789. 3 replies.

Sort Posts:

  • Authentication by Roles

    07-15-2009, 3:58 PM
    • Member
      1 point Member
    • saurabh29789
    • Member since 07-15-2009, 3:54 PM
    • Posts 4

    I have a Website that has 2 types of Roles -> User, Agent and Admin. I have enabled Forms Authentication and it's working well except for one problem as stated below :


    When  an authenticated "User" tries to enter  a page reserved for "Agent"s, or vice versa, he gets logged out, but what I want is that he should be sent back to his HomePage ( and be kept logged in). How shall I achieve this?

  • Re: Authentication by Roles

    07-15-2009, 6:09 PM
    Answer
    • Contributor
      5,226 point Contributor
    • sukumarraju
    • Member since 06-14-2006, 10:01 PM
    • Scotland, UK
    • Posts 1,146

    You can prevent users from accessing pages that are not contained within their roles as such: 

    //You can use the below code in Page_Load() event of the page 
    //You may use the same with "Admin" role and ~/AgentHome.aspx url 
    if (!User.IsInRole("Agent")   
                Response.Redirect("~/AdminHome.aspx");
    
    Let me know further Queries.
    -- "Mark As Answer" if my reply helped you --
    Recommended Book
    Application Architecture Guide 2.0
    My Blog
  • Re: Authentication by Roles

    07-15-2009, 7:12 PM
    Answer
    • All-Star
      17,222 point All-Star
    • guru_sarkar
    • Member since 08-31-2007, 12:00 AM
    • Posts 2,610

    the user is not logged-out in reality...but redirected to login.aspx...

    In your login page Load do like this:

    protected void Page_Load(object sender, EventArgs e)
    {
         if (!Page.IsPostBack)
         {
              if (Request.IsAuthenticated && !string.IsNullOrEmpty(Request.QueryString["ReturnUrl"]))
                         Response.Redirect("~/Home.aspx");
         }
    }



  • Re: Authentication by Roles

    07-16-2009, 1:41 PM
    • Member
      1 point Member
    • saurabh29789
    • Member since 07-15-2009, 3:54 PM
    • Posts 4

    Yes, I found out that the user was being redirected to the Login Page. 


    Problem Solved.

Page 1 of 1 (4 items)