<p>I have written a Silverlight 2 application communicating with a WCF service (BasicHttpBinding). The site hosting the Silverlight content is protected using a ASP.NET Membership Provider. I can access the current user using HttpContext.Current.User.Identity.Name from my WCF service, and I have turned on AspNetCompatibilityRequirementsMode. </p>

<p>I now want to write a Windows application using the exact same web service. To handle authentication I have enabled the <a href="http://msdn.microsoft.com/en-us/library/bb386582.aspx" rel="noreferrer">Authentication service</a>, and can call "login" to authenticate my user... Okey, all good... But how the heck do I get that authentication cookie set on my other service client?! </p>

<p>Both services are hosted on the same domain  </p>

<ul>
<li>MyDataService.svc &lt;- the one dealing with my data</li>
<li>AuthenticationService.svc &lt;- the one the windows app has to call to authenticate.</li>
</ul>

<p>I don't want to create a new service for the windows client, or use another binding...</p>

<p>The Client Application Services is another alternative, but all the examples is limited to show how to get the user, roles and his profile... But once we're authenticated using the Client Application Services there should be a way to get that authentication cookie attached to my service clients when calling back to the same server.</p>

<p>According to input from colleagues the solution is adding a wsHttpBinding end-point, but I'm hoping I can get around that...</p>

[{'answer_id': 57520, 'author': 'Jeremy McGee', 'author_id': 3546, 'author_profile': 'https://Stackoverflow.com/users/3546', 'pm_score': 2, 'selected': False, 'text': '<p>Web services, such as those created by WCF, are often best used in a "stateless" way, so each call to a Web service starts afresh. This simplifies the server code, as there\'s no need to have a "session" that recalls the state of the client. It also simplifies the client code as there\'s no need to hold tickets, cookies, or other geegaws that assume something about the state of the server.</p>\n\n<p>Creating two services in the way that is described introduces statefulness. The client is either "authenticated" or "not authenticated", and the MyDataService.svc has to figure out which. </p>\n\n<p>As it happens, I\'ve found WCF to work well when the membership provider is used to authenticate <em>every</em> call to a service. So, in the example given, you\'d want to add the membership provider authentication gubbins to the service configuration for MyDataService, and not have a separate authentication service at all.</p>\n\n<p>For details, see the MSDN article <a href="http://msdn.microsoft.com/en-us/library/ms731049.aspx" rel="nofollow noreferrer">here</a>.</p>\n\n<p>[What\'s very attractive about this to me, as I\'m lazy, is that this is entirely declarative. I simply scatter the right configuration entries for my MembershipProvider in the app.config for the application and! bingo! all calls to every contract in the service are authenticated.]</p>\n\n<p>It\'s fair to note that this is not going to be particularly quick. If you\'re using SQL Server for your authentication database you\'ll have at least one, perhaps two stored procedure calls per service call. In many cases (especially for HTTP bindings) the overhead of the service call itself will be greater; if not, consider rolling your own implementation of a membership provider that caches authentication requests.</p>\n\n<p>One thing that this <em>doesn\'t</em> give is the ability to provide a "login" capability. For that, you can either provide an (authenticated!) service contract that does nothing (other than raise a fault if the authentication fails), or you can use the membership provider service as described in the original referenced article.</p>\n'}, {'answer_id': 57760, 'author': 'Jonas Follesø', 'author_id': 1199387, 'author_profile': 'https://Stackoverflow.com/users/1199387', 'pm_score': 4, 'selected': True, 'text': '<p>I finally found a way to make this work. For authentication I\'m using the "<a href="http://msdn.microsoft.com/en-us/library/bb386582.aspx" rel="noreferrer">WCF Authentication Service</a>". When authenticating the service will try to set an authentication cookie. I need to get this cookie out of the response, and add it to any other request made to other web services on the same machine. The code to do that looks like this:</p>\n\n<pre><code>var authService = new AuthService.AuthenticationServiceClient();\nvar diveService = new DiveLogService.DiveLogServiceClient();\n\nstring cookieHeader = "";\nusing (OperationContextScope scope = new OperationContextScope(authService.InnerChannel))\n{\n    HttpRequestMessageProperty requestProperty = new HttpRequestMessageProperty();\n    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestProperty;\n    bool isGood = authService.Login("jonas", "jonas", string.Empty, true);\n    MessageProperties properties = OperationContext.Current.IncomingMessageProperties;\n    HttpResponseMessageProperty responseProperty = (HttpResponseMessageProperty)properties[HttpResponseMessageProperty.Name];\n    cookieHeader = responseProperty.Headers[HttpResponseHeader.SetCookie];                \n}\n\nusing (OperationContextScope scope = new OperationContextScope(diveService.InnerChannel))\n{\n    HttpRequestMessageProperty httpRequest = new HttpRequestMessageProperty();\n    OperationContext.Current.OutgoingMessageProperties.Add(HttpRequestMessageProperty.Name, httpRequest);\n    httpRequest.Headers.Add(HttpRequestHeader.Cookie, cookieHeader);\n    var res = diveService.GetDives();\n}      \n</code></pre>\n\n<p>As you can see I have two service clients, one fo the authentication service, and one for the service I\'m actually going to use. The first block will call the Login method, and grab the authentication cookie out of the response. The second block will add the header to the request before calling the "GetDives" service method.</p>\n\n<p>I\'m not happy with this code at all, and I think a better alternative might be to use "Web Reference" in stead of "Service Reference" and use the .NET 2.0 stack instead.</p>\n'}, {'answer_id': 62049, 'author': 'larsw', 'author_id': 2732, 'author_profile': 'https://Stackoverflow.com/users/2732', 'pm_score': 0, 'selected': False, 'text': "<p>It is possible to hide much of the extra code behind a custom message inspector &amp; behavior so you don't need to take care of tinkering with the OperationContextScope yourself.</p>\n\n<p>I'll try to mock something later and send it to you.</p>\n\n<p>--larsw</p>\n"}, {'answer_id': 451503, 'author': 'Chuck', 'author_id': 9714, 'author_profile': 'https://Stackoverflow.com/users/9714', 'pm_score': 0, 'selected': False, 'text': '<p>You should take a look at the <a href="http://msdn.microsoft.com/en-us/library/system.net.cookiecontainer.aspx" rel="nofollow noreferrer">CookieContainer</a> object in System.Net.  This object allows a non-browser client to hang on to cookies.  This is what my team used the last time we ran into that problem.</p>\n\n<p><a href="http://www.justskins.com/forums/session-state-and-winforms-consuming-a-web-service-9763.html" rel="nofollow noreferrer">Here is a brief article</a> on how to go about using it.  There may be better ones out there, but this should get you started.</p>\n\n<p>We went the stateless route for our current set of WCF services and Silverlight 2 application.  It is possible to get Silverlight 2 to work with services bound with TransportWithMessageCredential security, though it takes some custom security code on the Silverlight side.  The upshot is that any application can access the services simply by setting the Username and Password in the message headers.  This can be done once in a custom IRequestChannel implementation so that developers never need to worry about setting the values themselves.  Though WCF does have an easy way for developers to do this which I believe is serviceProxy.Security.Username and serviceProxy.Security.Password or something equally simple. </p>\n'}, {'answer_id': 850002, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>I wrote this a while back when I was using Client Application Services to authenticate against web services.  It uses a message inspector to insert the cookie header.  There is a word file with documentation and a demo project.  Although its not exactly what you are doing, its pretty close.  You can download it from <a href="http://www.baileysc.com/releases/clientappservicesdemo.zip" rel="nofollow noreferrer">here</a>.</p>\n'}, {'answer_id': 1101813, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>On the client modify your &lt;binding&gt; tag for the service (inside &lt;system.serviceModel&gt;) to include: allowCookies="true"</p>\n\n<p>The app should now persist the cookie and use it.  You\'ll note that IsLoggedIn now returns true after you log in -- it returns false if you\'re not allowing cookies.</p>\n'}]
['https://Stackoverflow.com/questions/56112', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1199387/']