Developer forum

Forum » Ecommerce - Standard features » How correctly use impesonation

How correctly use impesonation

Dmitrij Jazel
Reply

HI everyone,

I used to have a paragraph template that contained Login code blob 

<!--@If Defined(Global:Extranet.UserName)-->
            <em>You are logged in as:</em><br />
            <strong>
                <!--@Global:Extranet.Name-->
            </strong>
            <a href="/Admin/Public/ExtranetLogoff.aspx?ID=<!--@DwPageID-->">Logout...</a>
            <!--@EndIf(Global:Extranet.UserName)-->
            <!--@If Not Defined(Global:Extranet.UserName)-->
            <strong>Login</strong>
            <form id="login" action="Default.aspx" method="post">
                <input type="hidden" name="ID" value="<!--@DwPageID-->" />
                <label for="username">
                    Username</label>
                <input type="text" name="username" id="username" />
                <label for="password">
                    Password</label>
                <input type="password" name="password" id="password" />
                <input type="submit" value="Login" />
            </form>
            <!--@EndIf(Global:Extranet.UserName)-->

 

And in the paragraph where above this normally works, I changed this code with this impersonate code blob.

<!--@If(Global:Extranet.UserID==0)-->

<h3><!--@Translate(CustomerLogin, 'Customer login')--></h3>
    <div class="login-box-mode" data-login-box-mode="login">
        <!--@If(Global:Extranet.UserID==0)-->
            <!--@If(Server:Request.username<defined> || Server:Request.password<defined>)-->
            <div class="loginbox-login-failed alert alert-error"><!--@Translate(Invalid_username_or_password, "Invalid username or password")--></div>
            <!--@EndIf-->
        <!--@EndIf-->

        <form class="form-horizontal" method="post" onsubmit="">
            <input type="hidden" name="ID" value="<!--@Global:Page.ID-->" />                
            <div class="control-group">
                <input type="text" id="login-username" name="username" spellcheck="false" placeholder="<!--@Translate(Username, 'Username')-->" value="<!--@If(Server:Request.username<defined>)--><!--@Server:Request.username--><!--@EndIf-->"/>
            </div>
            <div class="control-group">
                <input type="password" id="login-password" name="password" placeholder="<!--@Translate(Password, 'Password')-->" value="<!--@If(Server:Request.password<defined>)--><!--@Server:Request.password--><!--@EndIf-->"/>
            </div>                
            <input type="submit" value="Login" />
        </form>
    </div>
<!--@Else-->
    <!--@Translate(LoggedInAs, 'Logged in as')--> <!--@DWextranet_AccessUserUserName-->
    <br />
    <br />
    <form method="post">
        <!--@DwTemplateTags-->
        <!--@If Not Defined(Global:Extranet.SecondaryUser.UserID)-->        
            <table border="0">            
                <tr>            
                    <td><!--@Translate(SelectUserToImpersonate, 'Select user to impersonate')--></td>
                </tr>
                <tr>
                    <td>
                        <!--@LoopStart(DWExtranetSecondaryUsers)-->                    
                            <!--@HeaderStart--><select id="DWExtranetSecondaryUserSelector" name="DWExtranetSecondaryUserSelector"><!--@HeaderEnd-->
                                <option value="<!--@UserID-->"><!--@UserName--></option>
                            <!--@FooterStart--></select><!--@FooterEnd-->                        
                        <!--@LoopEnd(DWExtranetSecondaryUsers)-->                                            
                    </td>
                </tr>            
                <tr>
                    <td colspan="2"><input type="submit" tabindex="3" value="    OK    "></td>
                </tr>            
            </table> 
        <!--@EndIf(Global:Extranet.SecondaryUser.UserID)-->          
        <!--@If Defined(Global:Extranet.SecondaryUser.UserID)-->            
            <h3><!--@Global:Extranet.SecondaryUser.UserName--> <!--@Translate(ImpersonatedBy, 'is impersonated by')--> <!--@DWextranet_AccessUserUserName-->.</h3>            
            <br />
            <input type="submit" name="DwExtranetRemoveSecondaryUser" id="DwExtranetRemoveSecondaryUser" value="Stop impersonation">
        <!--@EndIf(Global:Extranet.SecondaryUser.UserID)-->  
        <br />
        <br />
        <a class="btn" href="/Admin/Public/ExtranetLogoff.aspx?ID=<!--@DwPageID-->"><!--@Translate(Logout, 'Logout')--></a>
    </form>           
<!--@EndIf-->

I also setup corresponding settings and as instructed here: http://manual.dynamicweb-cms.com/Default.aspx?ID=7597

 

As a result - when I go to the page where this form is invoked - so I can login, but it shows noone as Impersonate options.

My question would be - anyone has any idea why this Could not function on standard dw 8,4,0,10?

And do I actually have to setup some of those (page properties > advanced > permissions) in order to make this work?

KInd regards,

Dmitrij

 


Replies

 
Dmitrij Jazel
Reply

Anyone tryed to use it?

Or is it so easy to use and I am the only one who has issues with it :-/

 
Merethe Nielsen
Reply

Hi

I have checked on my testsite with 8.4.0.14 and I get the people that I can impersonate when logged in:

http://screencast.com/t/HJzHayHQ

I have included the attached template in my page template: Include(LoginWithImpersonation.html)

Maybe you can try the same setup and see if you can get it to work?

Kind regards,
Merethe

 
Dmitrij Jazel
Reply

Hej Marethe,

Thank you for your screencast.

Only 1 question:

In your video - you navigated than rightclicked the page node, and selected view page.

I was wondering what paragraph, with what templatetemplate you have that calls this code.

I wanted to see the paragraphs you have on that page.

And do you use (Page > properties > advanced (tab) > Permissions) on this page?

 

The way how I invoke this code is by adding this code to a template file of customer center module.

Usually login form works just fine in there.

And I updated application to 8.4.0.15

http://screencast.com/t/MkeDmDKlTj

 

KInd regards,

Dmitrij

 
Merethe Nielsen
Reply

Hi

No I don't have permissions set on the page that I navigated to.

The DWExtranetSecondaryUsers loop is only available and rendered in page templates. Try move the loop to the page template. It is only rendered when the user is logged in.

Kind regards,
Merethe

 
Dmitrij Jazel
Reply

Hello again Merethe,

I added that code you sent me dirrectly to Index.html right now, and it did not worker eather :( And yes, I am able to login - when I login so can't see anyone I can impersonate. Got exactly the same result as in my precious post where I loggedin, but no users impersonation options got loaded :-(

Regards,

Dmitrij

 

 
Merethe Nielsen
Reply

That is weird. Du you have an url to the solution?

 
Dmitrij Jazel
Reply

unfortunetly not (not yet) it is local application that I have on my local machine, just to "try" impersonation.

 
Merethe Nielsen
Reply

Now I have tried with another solution that has never used impersonation. I copy the code from the LoginWithImpersonation.html template and inserted in the Condition Index template. Then I added people to impersonate.

In frontend I log in and it works as expected.

http://screencast.com/t/VISUtTdOTS

 

 
Dmitrij Jazel
Reply

Hej Marethe, I think I found the reason why I had this issue.

My site (I am am curretly working on this project here) is a  mix of HTML and Razor.

And some parts are coded in HTML some are in razor.

IF site is pure HTML, this html works just fine (that code snippet you sent me),  but if I try to use this template in Razor than it starts to make trouble.

 

I converted current HTML template into Razor (by myself no autoconvert used)

and I am getting an issue here:

HTML: <!--@If Not Defined(Global:Extranet.SecondaryUser.UserID)-->

Razor: @if (!string.IsNullOrWhiteSpace(GetGlobalValue("Global:Extranet.SecondaryUser.UserID"))) { ... }

And in only pure clean HTML it works correctly, but if I mix it with razor or razor/html templates it stops working.

Right now, @if (!string.IsNullOrWhiteSpace(GetGlobalValue("Global:Extranet.SecondaryUser.UserID"))) { ... } is not defined... but Razor executes the code that is supposed to be executed in else{...} statement.

 

Maybe something wrong with my razor template? Would be nice to get a Razor version along with HTML version in here: http://manual.dynamicweb-cms.com/Default.aspx?ID=7597 since we are to use Razor more.

 

/Dmitrij

 
Mikkel Ricky
Reply

You very rarely need global tags in Razor – probably never. Try using @Pageview.User.SecondaryUserID.

Best regards,
Mikkel

 
Dmitrij Jazel
Reply

Hej MIkkel! :-)

I am trying @if (!string.IsNullOrWhiteSpace(Pageview.User.SecondaryUserID)) {... }

It tells me 

Dynamicweb.Frontend.Extranet contains no definition for SecondaryUserID

... so far, GlobalValue was working just fine for me... but I must get a custom to use Pageview instead...

 

 
Mikkel Ricky
Reply

My bad. Pageview.User.SecondaryUserID is not public so you have to use GetGlobalTag for now.

What is the actual value of @GetGlobalValue("Global:Extranet.SecondaryUser.UserID")? The underlying value is actually an integer and if it's 0, string.IsNullOrWhiteSpace will return false. Try converting GetGlobalValue("Global:Extranet.SecondaryUser.UserID") to an integer and check if this integer is greater than 0.

Best regards,
Mikkel

 
Dmitrij Jazel
Reply

Hej Mikkel! :)

Sorry for comming back so late, but I was a bit busy lately...

So, last time as you suggested I did this:

int intVal = Convert.ToInt32(GetGlobalValue("Global:Extranet.SecondaryUser.UserID"));

Eventually I test it like this:

<text>intVal: @intVal</text>

And as a result I am getting an exception :-)

Exception in template (UserManagement/ViewProfile/view_profile.cshtml): System.FormatException: Inndatastrengen hadde ugyldig format.
   ved System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
   ved System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
   ved CompiledRazorTemplates.Dynamic.efbdefbcfcafac.Execute()
   ved RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context)
   ved RazorEngine.Razor.Parse[T](String razorTemplate, T model, String cacheName)
   ved Dynamicweb.Rendering.Template.Output()

Sorry for "norwegian" exception, but I am sure you should know what the exception is.

/Dmitrij

 

 
Mikkel Ricky
Reply

I said "Try converting GetGlobalValue("Global:Extranet.SecondaryUser.UserID") to an integer", i.e. use int.TryParse to prevent errors if the actual value is not an integer. What is the value of GetGlobalValue("Global:Extranet.SecondaryUser.UserID") in your case?

 
Dmitrij Jazel
Reply

Hej Mikkel :)

Hope you had a great easter! :)

 

Allright, so I tryed now to use int32.TryParse(...) and I tryed eventually to read the value

int SecUsrInt = Int32.TryParse(GetGlobalValue("Global:Extranet.SecondaryUser.UserID"), out SecUsrInt);
<text>testing Global:Extranet.SecondaryUser.UserID[@SecUsrInt]</text>

This gave me this exception

Line 26: Kan ikke konvertere typen bool til int

This means I have to parse it to Boolean :) and I did

bool bool_secUsrInt = bool.TryParse(GetGlobalValue("Global:Extranet.SecondaryUser.UserID"), out bool_secUsrInt);
<text>testing Global:Extranet.SecondaryUser.UserID[@bool_secUsrInt]</text>

Than this gave me "False" as a result, and this is fine - cause I am not yet impersonating anyone just yet.

 

Than I continue to this loop here:

<select id="DWExtranetSecondaryUserSelector" name="DWExtranetSecondaryUserSelector">
    @foreach (var item in GetLoop("DWExtranetSecondaryUsers")) {
        @: @item.GetValue("UserID")
    }
</select> 

And this loop shows me no options. But I did add Impersonation options in user management. This user has 2-3 users he should be able to impersonate.

 

No error messages so far, just no impersonation options...

 

//Dmitrij

 
Dmitrij Jazel
Reply

Hej Mikkel :-)

Is there anything else I could try? :-( Not sure if anyone else had this issue, But something deffenetly is not OK here.

The DWExtranetSecondaryUsers loop should give me some results, but it still comes out empty :-( any idea why?

 

//Dmitrij

 
Mikkel Ricky
Reply

I've just added a Razor version of the example template to the documentation page: LoginWithImpersonation.cshtml

Best regards,
Mikkel

 
Dmitrij Jazel
Reply

Hej Mikkel,

 

That is perfect! :) Thanks so much for that example, I now have impersonation working (so far so good) :-D

Now if I copy paste your code into a page template it works just fine :) so all is OK.

 

But I noticed one thing, not sure if that is intentional or not, but.

If you:

1) Create new blank site in razor.

2) Add a page with Razor index.cshtml template.

3) On that page add a paragraph with "Extranet/Intranet (Extended)" module for managing users.

4) In that "Extranet/Intranet (Extended)" paragraph module settings, Set "view profile" in Show section.

5) Than if you will use your impersonation code on "view_profile.cshtml" template - than you get the same issue I get. Even if impersonation works overall on website - you get no options in the impersonate dropdown.

 

In other words, DWExtranetSecondaryUsers loop stops working if you use it under "Extranet/Intranet (Extended)" > View Profile > view_profile.cshtml In Razor. You can try that and tell me if you get it to work, didn't work for me.

 

Otherwise I got it working through Page layout template. I guess I will use it there. Thanks for so far! :-)

Dmitrij

 
Mikkel Ricky
Reply

The DWExtranetSecondaryUsers loop is only available in places when a user can log in, i.e. on page templates and in login templates (under Page permissions). This is by design.

However, the design may be wrong, and we may have to reconsider. How do you intend to use the loop in View profile?

Best regards,
Mikkel

 
Dmitrij Jazel
Reply

hi Mikkel,

 

My original idea was to have a place on the website, where user could easilly overeview data. And manage it if needed.

For that reason I am using (Extranet/Intranet (Extended)) module.

After Login, user is redirected to a page where he/she can overview all their properties (view profile).

Since impersonation is dirrectly associated with user himself, I thought It was a great idea to maintain View profile and Impersonation code in one template (the switch select box). 

 

And another reason why I added it there in the first place was because this code snippet was not really dependent on anything particular. (Please correct me if I missed something).

Simmilar to login box, it works pretty much anywhere on the website and that includes view profile template,

 

Hope It makes sense :-)

 

/Dmitrij

 

You must be logged in to post in the forum