快捷搜索:

WCF服务中操作FormsAuthentication的Cookie

在asp.net 利用法度榜样和WCF办事之间共享FormsAuthentication,默认是不支持的,设置一下异常的简单,只必要两步就可以了:

1、在web.config的system.serviceModel增添一个设置设置设备摆设摆设:

2、为WCF打上标签

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

这样在WCF办事上就可以操作cookie了。

附注:

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

// 创建验证票

System.Web.Configuration.FormsAuthenticationConfiguration formsConfig = new System.Web.Configuration.FormsAuthenticationConfiguration();

FormsAuthenticationTicket formAuthTicket = new

FormsAuthenticationTicket(

1,// 版本

username,// 用户名称

DateTime.Now,// 创建光阴

DateTime.Now.AddMinutes(formsConfig.Timeout.TotalMinutes),// 掉效光阴

true,"");// 用户数据

//加密票

string encryptedTicket = FormsAuthentication.Encrypt(formAuthTicket);

// 以加密票的密文存入Cookie

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;

}

}

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/shanyou/archive/2009/09/06/4680978.aspx

您可能还会对下面的文章感兴趣: