Application in Asp.netProgramFormsauthentication is shared with the WCF Service. It is not supported by default. It is very simple to set up. It only takes two steps:
1. Add a configuration in system. servicemodel of Web. config:
<System. servicemodel>
<Servicehostingenvironment aspnetcompatibilityenabled = "true"/>
</System. servicemodel>
2. Tag WCF
[Aspnetcompatibilityrequirements (requirementsmode = aspnetcompatibilityrequirementsmode. Allowed)]
In this way, the cookie can be operated on the WCF Service.
Note:
WCF userauthenticate
[Servicecontract]
Public Interface Iuserauthenticate
{
[Operationcontract]
String Verifyuser ( String Username, String Password, String Appcode );
}
[Aspnetcompatibilityrequirements (requirementsmode = Aspnetcompatibilityrequirementsmode. Allowed)]
Public Class Userauthenticate: iuserauthenticate
{
Public String Verifyuser ( String Username, String Password, String Appcode)
{
VaR apprep = New Appsrepository ();
Appinfo app = Apprep. getapp (appcode );
If (App = Null )
Return Null ;
Loginuserstatus loginstatus = Loginuserstatus. success;
If (Loginstatus = Loginuserstatus. Success)
{
System. Web. Security. formsauthentication. setauthcookie (username, True );
// Create verification ticket
System. Web. configuration. formsauthenticationconfiguration formsconfig = New System. Web. configuration. formsauthenticationconfiguration ();
Formsauthenticationticket formauthticket = New
Formsauthenticationticket (
1 , // Version
Username, // User Name
Datetime. Now, // Creation Time
Datetime. Now. addminutes (formsconfig. Timeout. totalminutes ), // Expiration time
True , "" ); // User Data
// Encrypted ticket
String Encryptedticket = Formsauthentication. Encrypt (formauthticket );
// Store the cookie in the ciphertext of the encrypted ticket
Httpcookie authcookie = New Httpcookie (formsauthentication. formscookiename, encryptedticket );
Authcookie. HTTPOnly = True ;
Authcookie. Path = Formsauthentication. formscookiepath;
Authcookie. Secure = Formsauthentication. requiressl;
If (Formsauthentication. cookiedomain ! = Null )
{
Authcookie. Domain = Formsauthentication. cookiedomain;
}
If (Formauthticket. ispersistent)
{
Authcookie. Expires = Formauthticket. expiration;
}
Httpcontext. Current. response. Cookies. Add (authcookie );
Formsidentity identity = New Formsidentity (formauthticket );
Genericprincipal principal = New Genericprincipal (identity, Null );
Httpcontext. Current. User = Principal;
Passportticket ticket = New Passportticket (formauthticket, encryptedticket, APP );
If (Ticket. Save ())
{
Return Ticket. Ticket. publicticket;
}
Return Null ;
}
Return Null ;
}
}