Print Page | Close Window

Entitymanager server side

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2525
Printed Date: 29-Jul-2026 at 4:58pm


Topic: Entitymanager server side
Posted By: danjal
Subject: Entitymanager server side
Date Posted: 22-Feb-2011 at 8:23am

We have begun using a great control called Pivotviewer. We see this as a control that can be used in many situations. http://www.silverlight.net/learn/pivotviewer/ - http://www.silverlight.net/learn/pivotviewer/

 

We use Silverlight DevForce, with a login manager, in a PRISM environment.

One of the challenges we encountered was that we had to develop some HttpHelpers that generate just-in-time collections to the Pivotviewer The Pivotviewer loads the collection from a .cxml file which is given by an url. The file does not exist in our case so the HttpHelper generates it on-the-fly and returns the collection. This code runs server side and so we had problems creating an Entity manager server side:

var m_entityManager = new C2NetDomainModelEntityManager();

 

Reason is that the login manager lacks credentials – a user is logged in client side thou.

Our workaround was to add some code to the login manager. We put the logged in userId in the Tag property on the default manager when a user logs in:

EntityManager.DefaultManager.Tag = user.p_userID; // so I can see how is logged in later.

 

On our server, when generating the collection, we use the default manager. But this does not give us our EntityModel – so we have to type cast everything:

var m_entityManager = EntityManager.DefaultManager;

               

var query = new EntityQuery<EntImage>()

                    .Include("p_imageAlbums")

                    .Include("p_imageAlbums.p_imageAlbumType");

query.QueryStrategy = new QueryStrategy(FetchStrategy.DataSourceOnly, MergeStrategy.OverwriteChanges);

 

var images = m_entityManager.ExecuteQuery<EntImage>(query);

 

And then we use the Tag property of the default manager to do some access management:

if (album.p_usersWithAccess.Where(user => user.p_userID == (string) m_entityManager.Tag).Count() > 0)

hasAccessToImage = true;

 

Is this the right way doing it, when working server side?

Or is there a better way?




Replies:
Posted By: DenisK
Date Posted: 22-Feb-2011 at 7:38pm
Hi danjal;

When you say server-side, which server-side operation do you mean? Are you doing the access management inside a server method, or the query or save interceptor?


Posted By: smi-mark
Date Posted: 23-Feb-2011 at 9:38am
If you are doing this inside a remote server method, you are already passed an EntityManager, which you can use as you are now, or create a new one  by doing something like:

var manager = new C2NetDomainModelEntityManager(entityManager);

You are also passed a principal object through the remote server method, so I'm not sure why you need to be tagging the EM.

Perhaps this will help:

http://drc.ideablade.com/xwiki/bin/view/Documentation/InvokeServerMethod


Posted By: danjal
Date Posted: 23-Feb-2011 at 9:40am

Our HttpHandlers run in our webproject. They handle requests for any .cxml files.

When handling the request, we want to know you is logged in to the silverlight application, that is sending the request.

When handling the request, we need to run queries against our entitymodel to generate some data for the .cxml file.



Posted By: smi-mark
Date Posted: 23-Feb-2011 at 9:48am
Ok that changes things. If you try this in your handler:

var m_entityManager = new C2NetDomainModelEntityManager();
m_entityManager.Login(null);

Does the returned principal indicate the silverlight user? I'm not sure if you are using ASP.Net security or not, if not, how are you retrieving the user currently?


Posted By: danjal
Date Posted: 23-Feb-2011 at 10:03am

When the user logs into to the silverlight application, I set:

EntityManager.DefaultManager.Tag = user;

, within the login manager.

When a request comes in, I create the manager this way:

var m_entityManager = EntityManager.DefaultManager;

 

And get the user this way:

var user = m_entityManager.Tag;

 

This is probably not good enough because I have NOT tested this:

User A logs in and requests HIS collection.

User B logs in and requests HIS collection.

User A makes another request to get HIS collection.

 

I am not sure about the ASP.Net security stuff – I will look into that.



Posted By: DenisK
Date Posted: 23-Feb-2011 at 2:57pm
Hi danjal;

I'm assuming that in your custom HttpHandler, you're implementing IHttpHandler interface which requires you to implement the following method

void ProcessRequest(
	HttpContext context
)
Looking at the HttpContext object, I believe that you can access the current user through HttpContext.User property.
 http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user.aspx - http://msdn.microsoft.com/en-us/library/system.web.httpcontext.user.aspx 


Posted By: danjal
Date Posted: 24-Feb-2011 at 3:23am

Thanks DenisK & smi-mark,

 

I have some reading to do. But in the mean while I ended doing something like this and it works:

 

When making the request, I attach this to the url:

„MyCollection.cxml?user=“ + someUserId;

 

And when handling the request I do like this:

// create a entitymanager

var m_entityManager = new C2NetDomainModelEntityManager(EntityManager.DefaultManager);

 

// retrieve the user from the querystring

var userid = context.Query["user"];

 

// check if there is a user

if(string.IsNullOrEmpty(userid))

throw new Exception("No access!");

....

.....

..

 

PS.

The HttpContext.User is the user that’s logged in on my machine. I have to do some reading regarding changing this when working with a Silverlight application.



Posted By: smi-mark
Date Posted: 24-Feb-2011 at 8:31am
Hi Danjal,

I would NOT do that as it would be very easy to fake the user.

I have created a sample for you that does what you need.

http://cid-e0769a20a6658b5a.office.live.com/self.aspx/IdeaBlade/HandlerTest.zip

If you look in the LoginManager.cs in the web project, I am simply allowing any user to authenticate at the moment. In the MainPageViewModel.cs you will see I am logging in as "myusername".

When you start the project it will login and then dump the output from the web handler (TestHandler.ashx) which simply shows the current username.

This is all possible because we are using forms authentication.

Hopefully this helps :)


Posted By: danjal
Date Posted: 24-Feb-2011 at 9:01am

Smi-mark,

 

Thank you so very much!!!

This is very helpful!!! J



Posted By: smi-mark
Date Posted: 24-Feb-2011 at 3:39pm
Glad that works for you!



Print Page | Close Window