Cookie processing in sl oob Mode

Source: Internet
Author: User
Originally, we wanted to implement the single-point logon mode in sl oob mode. The server uses the authenticationdomainservice. in IE (Browser networking stack), everything works normally. However, in OOB mode (Client networking stack)You can only log on again each time, but cannot share cookies. You can check some information on the Internet. This problem cannot be solved, and is a natural mechanism.

The following is the core code for Cookie processing.

 

 

Using system; using system. net; using system. windows; using system. windows. controls; using system. windows. documents; using system. windows. ink; using system. windows. input; using system. windows. media; using system. windows. media. animation; using system. windows. shapes; using system. servicemodel. domainservices. client; using system. servicemodel; using system. servicemodel. channels; using system. servicemodel. descriptio N; using system. servicemodel. dispatcher; namespace clinetnetstacktest {public static class implements {static readonly sharedcookiehttpbehavior sharedcookiebehavior = new behavior (); public static void implements cookiecontainer (domaincontext context) {var channelfactoryproperty = context. domainclient. getType (). getproperty ("channelfactory"); If (channelfactorypropert Y = NULL) throw new invalidoperationexception ("there is no 'channelfactory' property on the domainclient. "); var factory = (channelfactory) channelfactoryproperty. getvalue (context. domainclient, null); (custombinding) factory. endpoint. binding ). elements. insert (0, new httpcookiecontainerbindingelement (); factory. endpoint. behaviors. add (sharedcookiebehavior);} class sharedcookiehttpbehavior: webhttp Behavior {readonly extends inspector = new sharedcookiemessageinspector (); Public override void applyclientbehavior (serviceendpoint endpoint, clientruntime) {clientruntime. messageinspectors. add (inspector);} class sharedcookiemessageinspector: iclientmessageinspector {public sharedcookiemessageinspector () {} static cookiecontainer = new coo Kiecontainer (); Public void reply (ref message reply, object correlationstate) {# If debug httpresponsemessageproperty httpresponse = reply. properties [httpresponsemessageproperty. Name] As httpresponsemessageproperty; if! = NULL) {string cookie = httpresponse. headers ["Set-cookie"]; // HTTPOnly if (! String. isnullorempty (cookie) {MessageBox. show ("Cookie received" + Cookie) ;}# endif }} public object beforesendrequest (ref message request, iclientchannel channel) {channel. getproperty <ihttpcookiecontainermanager> (). cookiecontainer = cookiecontainer; return NULL ;}}}}

 

Add the following code to the SL app:

  if (Application.Current.IsRunningOutOfBrowser)            {               bool b= WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);               b= WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);            }

There is another key point to deal with. Rewrite the domianservice oncreated code, and enter the cookie processing logic into the injection. Then, the cookie will be transmitted during the interaction process.

using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;namespace ClinetNetStackTest.Web.Services{    public partial class AuthenticationDomainContext    {        partial void OnCreated()        {            if (Application.Current.IsRunningOutOfBrowser)                ClientHttpAuthenticationUtility.ShareCookieContainer(this);        }    }}

 

 

The server code is as follows:

Using system; using system. collections. generic; using system. componentmodel; using system. componentmodel. dataannotations; using system. LINQ; using system. servicemodel. domainservices. hosting; using system. servicemodel. domainservices. server; using system. servicemodel. domainservices. server. applicationservices; using system. web; namespace clinetnetstacktest. web. services {[enableclientaccess] public class Authen Ticationdomainservice: authenticationbase <user >{// to enable forms/Windows authentication for a web application, edit the corresponding part of the Web. config file. Public String getoobusername () {If (httpcontext. current. request. cookies. count> 0) {return httpcontext. current. request. cookies [system. web. security. formsauthentication. formscookiename]. value;} else {return "not send cookie" ;}} public bool isauthenticated () {return httpcontext. current. user. identity. isauthenticated ;}}}

Identity can be obtained between methods when a service is called, but the single point of failure (spof) cannot be achieved.
After several days of study, this is the only time to complete.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.