Silverlight, WCF AspNetCompatibilityRequirements and ASPNET Session State not shared - wcf

I have a Silverlight application that on a button opens an aspx page in a new browser window.
I want to pass some username/password details from Silverlight to the aspx page. My understanding is that I should be using WCF services to set the session state, which can then be retrieved from the aspx page.
I have followed what I think are the correct steps, but the aspx page refuses to see the state set by Silverlight.
What I have done is;
Created a WCF service that sets System.Web.HttpContext.Current.Session["Thing"]
On that service, set AspNetCompatibilityRequirements = Required and also set aspNetCompatibilityEnabled="true" on the web.config
My Silverlight application is able to set (and retrieve) session information using this WCF service successfully.
However when I get Session["Thing"] on the aspx page it is blank. Also if I set Session["Thing"] on the aspx page, Silverlight does not get it.
It's as if the two have different sessions - why is this?
Thanks in advance
Matt

Is it possible that the WCF service that your Silverlight client talks to is in a different web app (ie, is there more than one web project)? The default in-proc state provider is really per-appdomain, so if they're in different apps, you'd have two copies of your session state in two appdomains. If this is the case, just move the service code into the same webapp with the pages, and life is good. I can't think of any other explanation- I've done this plenty of times with no trouble.

As far as I can tell, if you open the page in a new browser window, it will create a new session. Your only hope of having the session shared is to create a shared object at the web server level (ugly).
Why don't you just pass the state as a querystring? Or open the page in an HtmlHost link text element withing SL?

Related

Load Silverlight combo-box with data from a Entity Model in Host ASP.NET MVC application

I have a particular scenario which has defied all attempts I made at handling it. I have a Silverlight application hosted in an ASP.NET MVC application. This ASP.NET application has an ADO.NET Entity model that connects to a database.
A combo box on the Silverlight page has a combo box that should be loaded with data when the ASP.NET page that hosts the silverlight application loads. The data that it should contain should come from the data in the ASP.NET application that hosts the silverlight app.
I thought of putting the data in a session within the ASP.NEt app's controller action that returns the view that hosts the silverlight app, hoping I could load it in the Load method of the Silverlight app but Sessions do not exist in silverlight.
I have equally tried WCF, Data Services, WCF RIA Library, Domain Service Class and I kept getting an error like "An error occured while trying to download service info". Please what is the best way to achieve this? Thank you.
Here I found some material through these links:
http://social.msdn.microsoft.com/Forums/silverlight/en-US/cc195a53-f596-43de-9d99-8d6a6ed1ced0/how-to-serialize-a-object-to-xml-in-silverlight
http://social.msdn.microsoft.com/Forums/silverlight/en-US/f090ee80-4574-40e4-98c3-fd3049a3e4ec/how-to-deserilize-the-serialized-xml-into-it-original-form-of-object-?forum=silverlightnet
http://www.dotnetglobe.com/2011/07/passing-objects-between-aspnet-and.html
Grateful for this platform S.O.

Is it possible to create a website with WCF?

Is it possible to create a website (hosted locally on my machine is fine for now) using a WCF Service Application?
By "web site" I mean allow an HTTP call from a web browser to my service and have the service return an HTML page that the browser can then render/display.
I am doing something very similar using a WCF based REST service. I have a WCF service that is accessed only from a web browser to download images. If you browse to 'http://www.MyFooImageService.com/100', it will lookup from the database an image with ID 100 and serve it up to the user. I use it to serve images from WCF service for the purpose of sending (user defined) emails with embedded images.
I used this guide with great results: RESTFUL WCF Service Step-ByStep. Check it out and see if a REST based WCF service is what you are looking for.
No, it doesn't quite work like that. WCF applications are service applications. There is no GUI interface or web interface or any kind of interface at all.
What you do is you create a WCF service that does some sort of function. Lets say you create a method called StoreName that stores your name into a database. Keep it simple. WCF services can be hosted in a variety of ways, depending on how you plan to use it. But to keep it simple, lets say you host the application using IIS.
Now this WCF application has no interface for interacting with it. You need to create an ASP.NET application (or it can be PHP, or jQuery if you prefer). It might have a text box and a submit button. Once the submit button is pushed, the ASP.NET application (or jQuery or PHP) makes an ajax call to your WCF service, passing it in the name as a POST parameter. The WCF service then does the work of storing it in the database. Execution then returns back to your web application.
This is a general overview of how this works. I hope it helps!
A website involves:
1) A user requesting HTML from somewhere
You can proxy pass a simple HTTP Get request to a WCF service hosted in a console app (or hosted in IIS), and configure it to return an HTTP request of content type text/html. Then the user would see a website appear in the browser.
But WCF is not a good fit for this. Especially if it's a simple HTML page. Others have said ASP.NET is better for this. And that's true. But node.js or PHP or pretty much anything that isn't .NET is MUCH better for this.
2) A user uses the web page to interact with server processes
This involves a user clicking a button or moving a mouse, or anything, and then that results in the web page (actually a web app at this point) making AJAX requests to one, or several server(s).
WCF is quite a good fit for this.

WCF FederatedAuthentication session state messes with ClaimsAuthorizationManager

Context:
There are a few threads here on Stack and on Bing that talk about WCF and session sharing with ASP.NET. None satisfy my issue fully. So here goes:
I have an MVC site which also has a few services in the RouteTable under the path "/services". This all works really nice. I can call Controller actions and WCF service methods both.
I have enabled WIF and Federated Authentication with the Windows Azure Access Control Service (ACS). This also works nicely. I can login using Facebook, Google etc. and a session is clearly established on my site. I can call the MVC controller actions and the WCF methods. Inside the body of those the IPrincipal for the current user is set to my current session.
Now I want to be a good boy and use a custom ClaimsAuthenticationManager and a custom ClaimsAuthorizationManager. A weird problem now occurs:
The setup:
For this scenario I am logged in. I have a session. I can confirm this inside of the method bodies of my MVC controller methods and my WCF service methods. This means we can leave the custom ClaimsAuthenticationManager out of this since it is not being touched when I already have a session.
It's the custom ClaimsAuthorizationManager which causes my headache.
When I make a call to the MVC site I am authenticated with my session inside of the ClaimsAuthorizationManager.CheckAccess method AND also inside of the body of my MVC controller actions. So far so good.
The problem:
When I make a call to the WCF services I am NOT authenticated with my session inside of the ClaimsAuthorizationManager.CheckAccess method but I am authenticated (suddenly) as I hit my breakpoint inside of the WCF service method.
This just does not make any sense! It seems my session is not inflated inside of the ClaimsAuthorization manager but further down the pipeline when I hit my own WCF code the session is in place!
How can this be?
Cheers,
Magnus
It might be the Stackoverflow's problem, but when I copied your config into notepad++ I saw some junk characters in the AuthenticationManager type value:.
As the rest of the question goes, do you have <clear /> element before the elements that you show here?

Using a console application to host WCF endpoints that expose asp.net ProfileService, ProfileService and RoleService

I've got an MVC web application that is used as an interface to a Console based app that exposes a bunch of ServiceHost/s using the net.pipe protocol.
I would like to use the asp.net membership/role/profile provider to manage my users and their roles and profile information (Inside the Console Application). I've done this in quite a few apps, but normally I reference these providers directly from the web application itself.
This is a good walk-through on doing pretty much what I would like, except I don't want to host the WCF service endpoints in IIS, but inside my console app - which will eventually become a windows service. When I try and host the ServiceHost through my console application I get the following error:
This service requires ASP.NET compatibility and must be hosted in IIS.
Either host the service in IIS with ASP.NET compatibility turned on in
web.config or set the
AspNetCompatibilityRequirementsAttribute.AspNetCompatibilityRequirementsMode
property to a value other than Required.
Now it seems that I won't be able to set that property to anything other than Required.
I've tried another route which is using the wrapper class/interface defined here for my authentication service, which I managed to get wired into in my MVC app without too much trouble, but this doesn't cover my Authorisation (using roles) or profile needs.
Has anyone got a solution to this, I can't be the only one trying to do this? I'm not

ASP.NET, SilverLight, WCF & Forms Authentication - How to configure endpoints?

I have this existing environment:
1) ASP.NET 3.5 web application
2) forms authentication with the SqlMembershipProvider
I need to add the following:
1) a Silverlight charting object embedded in a web page.
2) a WCF service to be consumed by:
a) the Silverlight component embedded in an authenticated
web page, as mentioned above
b) server-based WCF clients with certificate based authentication
My question is - what is the easiest/best way to configure the WCF endpoints for the Silverlight object to authenticate to the WCF service using the security context of the already logged-in user (via the page that’s hosting the Silverlight object) without having to use the user's username/password again?
I've researched a lot of the MSDN and Patterns & Practices material and I thought I had a decent grasp of many of the potential authentication scenarios. But I can't seem to figure out a way to tie them together for the scenario I've outlined. I've found other people posting similar questions to mine but none of the answers I've read seem to fully answer their questions either. (Maybe I'm making this harder than it needs to be?)
I would think that the solution would be to somehow use the authentication token/cookie generated in the asp.net form login and somehow pass that to the Silverlight object which then includes it in the WCF request. But I don't see how to configure the WCF endpoint to use that token.
(In some of my other projects I've implemented the server-to-server scenario in 2.b above using certificate-based authentication, so I'm not too worried about adding that to the current mix I've outlined.)
Thanks in advance for any insight or pointers to the path forward.
Terry
Thanks codemeit for trying to help but I finally figured out what I was doing wrong - it was pilot error.
In trying to configure the endpoints for my Silverlight app I was testing with an asp.net page. I finally realized that when I test that way, the client endpoint is no longer originating from the authenticated browser - the client endpoint is the IIS server which in turn executes the request against the WCF server endpoint. So the security context changes and HttpContext.Current.User.Identity is always empty at the WCF server endpoint.
Once I got my test SL app running in the browser, it automatically inherited the security context of the authenticated browser and then HttpContext.Current.User.Identity was correct and authenticated at the WCF server endpoint.
Have you tried to enable your WCF services with aspNet compatibility, then see if the following is true.
string currentUserName = HttpContext.Current.User.Identity.Name;
bool isLoggedIn = HttpContext.Current.User.Identity.IsAuthenticated;
if these properties are being populated with the expected values, then this is the one you are after.
To enable aspNet Compatibility
add to web.config
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
add to the service impl class
[AspNetCompatibilityRequirements
(RequirementsMode=AspNetCompatibilityRequirementsMode.Required)]
In this case, the endpoint would be using basicHttpBinding, and you could check the authentication at run time within WCF.