tags:

views:

24

answers:

1

Hi,

I'm creating a site with 2 different sections (main site and admin) and both of them need authentication.

I have the main section already created and it works fine using FormsAuthentication.

now, how do I go about creating the admin section? Can I use FormsAuthentication again?

thanks

A: 

user - yes you can.

what you need to do is to create roles (such as webuser and admin) and assign the user to the appropriate role as required (you can do this either when setting the user up initially or later as an edit on their profile). anyway, getting back to the question. inside your controller, you'd then investigate the roles that existed for that logged in user and this would determine which controller actions they had access to as well as determining which view to present, should the action be 'shared' between roles.

within the controller, you can decorate the action with the following code:

[Authorize(Roles="admin")] 
public ActionResult IndexAdminOnly() // you'd never have an action named this - purely to make the point
{
    // your logic here
}

conversely, you could do it inside the controller:

[Authorize] 
public ActionResult Index() 
{
    if(Roles.IsUserInRole("admin")){
    // your admin logic here
    }
    if(Roles.IsUserInRole("webuser")){
    // your webuser logic here
    }
}

this is it at it's very simplest. hopefully you can google a few more links to get you over any issues that arise once you get going, or drop a note here.

cheers

jim

jim
hey, thanks for that. I think I forgot to mention that I will need 2 separate login areas. one for the admin area and the other for the main site.So basically if anyone goes somewhere under /Admin the users will have to be redirected to the admin login screen and for users to access the main section they will have their separate login screen.How do I manage to achieve that? Can you set up 2 of these in the web config to redirect users accordingly? <authentication mode="Forms"> <forms loginUrl="~/Home/Index" timeout="30" /> </authentication>thanks
user
user - 'fraid i can't help you there. we use a single login policy here and use the user roles to determine what functionality is available based on that.
jim