+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Programatically set impersonate user

Programatically set impersonate user

7 posts, 0 answered
  1. MJia
    MJia avatar
    44 posts
    Registered:
    09 Jun 2009
    22 Apr 2010
    Link to this post
    Hi,
    I created a role in sitefinity called 'agent', and add a user 'agentonly' in this role.  And most of the pages are for public and some of them are set as only viewable by 'agent' role.

    And we have customized a navigation user control which can retrieve menu items from Sitefinity sitemap provider.  If we didn't login as 'agentonly', those agent only pages won't be showed in the navigation.  We want the user control to have the ability to get those agent only pages and show them in the navigation.

    And further, if we create more roles, and pages set as only viewable by different roles, we add a property for our control, and based on the property setting we want our control to get pages accordingly.  (e.g., we add a 'TheRole' property for the control, if it is set to 'agent', then return public pages and agent only pages, if it is set to 'wholesale', then return public pages and wholesale only pages)

    How should I program this?

    Thanks.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    23 Apr 2010
    Link to this post
    Hi MJia,

    You could create a public property to enter the role names. Then in ItemDataBound event of your Navigation control (I suppose that you will use RadMenu or PanelBar) you could read the role property, get the roles for the currently authenticate user by calling UserManager.GetCurrentUserRoles and then decide whether to hide the navigation items or not.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. MJia
    MJia avatar
    44 posts
    Registered:
    09 Jun 2009
    23 Apr 2010
    Link to this post
    Thanks Ivan.
    I am sorry I didn't make the issue clearly described.
    Let me try again,
    1. We have a separate authentication system to verify which user is logged in and his role, those user accounts are in another database and we DONOT want to import them into Sitefinity; the authentication code is written in the master page;
    2. Our navigation control is simply using a treeview, and in code behind we work on Sitemap class to produce the menu items;
    3. In Sitefinity, we added 'agent' role and 'agentonly' user, and assign view permission for those pages that can only be viewed by the 'agent' role;
    4. Our goal is:  if a user logs in by the separated authentication system described in 1, he will be able to see all the public pages and 'agent' only pages;  if a user doesn't log in, then he can only see public pages
    5. Currently we use a different template master page for 'agent' only pages, in this master page we verify the user's role and decide whether to show the content, it is working properly
    6. The issue is: our navigation contorl is always showing the public pages only, because the Sitemap only returns the nodes that are viewable for everyone (If we use sitefinity login page to log in as 'agentonly' user, then the Sitemap returns the pages that we want)

    So the possible solution may be:  in our code behind when authenticating the logged in user, if his role is 'agent', then programmatically log in sitefinity as 'agentonly' user, then generate the navigation tree, and after that, programmatically log out 'agentonly' user.  

    However we don't know how to "programmatically log in / log out sitefinity".
    Or if you have other better options, please let me know.

    I hope that explain our issue clearly.

    Thanks.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    27 Apr 2010
    Link to this post
    Hi MJia,

    4. Our goal is:  if a user logs in by the separated authentication system described in 1, he will be able to see all the public pages and 'agent' only pages;  if a user doesn't log in, then he can only see public pages


    Depending on the current user you could bind the RadTreeView and build its nodes in NodeBound event of the control.

    So the possible solution may be:  in our code behind when authenticating the logged in user, if his role is 'agent', then programmatically log in sitefinity as 'agentonly' user, then generate the navigation tree, and after that, programmatically log out 'agentonly' user.


    You could logout a given user by clearing its login cookie and role cookie.

    myCookie.Expires = DateTime.Now.AddDays(-1d);
    Response.Cookies.Add(myCookie);


    Greetings,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  5. MJia
    MJia avatar
    44 posts
    Registered:
    09 Jun 2009
    27 Apr 2010
    Link to this post
    Hi,

    So the possible solution may be:  in our code behind when authenticating the logged in user, if his role is 'agent', then programmatically log in sitefinity as 'agentonly' user, then generate the navigation tree, and after that, programmatically log out 'agentonly' user.


    You could logout a given user by clearing its login cookie and role cookie.

    But how could I login a user programmatically?

    Depending on the current user you could bind the RadTreeView and build its nodes in NodeBound event of the control.

    Our navigation control is built by regular treeview, the nodes are retrieved from Sitemap provider.  How do I determine which role has access to which node programmatically?  I cannot hardcode the menuitems because they will be changed frequently.

    Thanks.
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    27 Apr 2010
    Link to this post
    Hello MJia,

    Below is a sample code that you could use the programmatically log in a user

    MembershipUser user = Membership.GetUser(qs);
    if (user != null)
    {
        FormsAuthentication.SetAuthCookie(user.UserName, true);
        HttpCookie c = FormsAuthentication.GetAuthCookie(user.UserName, true);
        UserManager.Default.SetAuthenticationCookie(c);
        Response.Redirect("~/Sitefinity/Admin/Default.aspx");
    }

    As for the other request, you need to use some identifier that will allow to show/hide nodes depending on the role of the current user.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  7. MJia
    MJia avatar
    44 posts
    Registered:
    09 Jun 2009
    27 Apr 2010
    Link to this post
    As soon as I call this:
    MembershipUser user = Membership.GetUser(qs);

    The page is redirected to sitefinity login page.

    Any solutions?
Register for webinar
7 posts, 0 answered