using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.Web.Security;
using System.Web;
using System.Net;
namespace WcfRestService5
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class LoginService
    {
         [WebInvoke(UriTemplate = "", Method = "POST",BodyStyle=WebMessageBodyStyle.WrappedRequest)]
        public bool Login(string userName, string userPassword)
        {
             
             //模拟登录，实际使用时需数据库访问
            if (userName == "huangbo" && userPassword == "123")
            {
                //登录成功，需手工生成cookie，写回http响应报头
                var ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(10), true, userName);
                string encryptedTicket =
                    FormsAuthentication.Encrypt(ticket);
                HttpContext.Current.Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket));
                return true;
            }
            return false;
        }

         [WebGet(UriTemplate = "",ResponseFormat=WebMessageFormat.Json)]
         public void ShowError()
         {
             throw new WebFaultException<string>("非法用户", HttpStatusCode.Unauthorized);   
         }
    }
}