在asp.net 应用程序和WCF服务之间共享FormsAuthentication,默认是不支持的,设置一下非常的简单,只需要两步就可以了:
1、在web.config的system.serviceModel增加一个配置:
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</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;
}
}
[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;
}
}