Cookie used to operate formsauthentication In the WCF Service

Source: Internet
Author: User

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 ;
}
}

 
 

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.