对于简单的安全性不高的wcf如果寄宿在IIS中wcf的验证完全可基于asp.net 的窗体验证http://blog.csdn.net/shanyou/archive/2009/09/06/4680978.aspx
该文对“WCF服务中 *** 作FormsAuthentication的cookie” *** 作有详细的说明
// 建立user wcf锲约 [ServiceContract(namespace = "" )] public interface IUser { [OperationContract] LoginMessage DoWork( string name); [OperationContract] LoginMessage Login( string username, string pass); [OperationContract] voID SignOut(); } /// <summary> /// login DataContract /// </summary> [DataContract] public class LoginMessage { [DataMember] public string Text; } // 实现接口 // 注意: 如果更改此处的类名 "User",也必须更新 App.config 中对 "User" 的引用。 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class User : IUser { public LoginMessage DoWork( string name) { LoginMessage a = new LoginMessage(); if (UserAuthenticate.isAuthenticate()) { a.Text = " hello " + httpContext.Current.User.IDentity.name.Trim(); } else { a.Text = " notlogin " ; } return a; } public LoginMessage Login( string username, string pass) { LoginMessage a = new LoginMessage(); if (username == " xgr2004 " && pass == " 123456 " ) { UserAuthenticate.VerifyUser(username,pass); a.Text = " true " ; } else { a.Text = " false " ; } return a; } public voID SignOut() { UserAuthenticate.SignOut(); } // 验证部分,这里拷了我给出连接 public class UserAuthenticate { static public string VerifyUser( string username, string password) { 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; return "" ; return null ; } static public bool isAuthenticate() { return httpContext.Current.User.IDentity.IsAuthenticated; } static public voID SignOut() { FormsAuthentication.SignOut(); httpContext.Current.Session.Clear(); } }
源代码下载 代码的相关介绍: 网上查阅了相关WCF的例子,一般都要证书,对于简单的安全性不高的wcf如果寄宿在IIS中wcf的验证完全可基于asp.net 的窗体验证http://blog.csdn.net/shanyou/archive/2009/09/06/4680978.aspx 该文对“WCF服务中 *** 作FormsAuthentication的cookie” *** 作有详细的说明 C# code // 建立user wcf锲约 [ServiceContract(namespace = "" )] public interface IUser { [OperationContract] LoginMessage DoWork( string name); [OperationContract] LoginMessage Login( string username, string pass); [OperationContract] voID SignOut(); } /// <summary> /// login DataContract /// </summary> [DataContract] public class LoginMessage { [DataMember] public string Text; } // 实现接口 // 注意: 如果更改此处的类名 "User",也必须更新 App.config 中对 "User" 的引用。 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class User : IUser { public LoginMessage DoWork( string name) { LoginMessage a = new LoginMessage(); if (UserAuthenticate.isAuthenticate()) { a.Text = " hello " + httpContext.Current.User.IDentity.name.Trim(); } else { a.Text = " notlogin " ; } return a; } public LoginMessage Login( string username, string pass) { LoginMessage a = new LoginMessage(); if (username == " xgr2004 " && pass == " 123456 " ) { UserAuthenticate.VerifyUser(username,pass); a.Text = " true " ; } else { a.Text = " false " ; } return a; } public voID SignOut() { UserAuthenticate.SignOut(); } // 验证部分,这里拷了我给出连接 public class UserAuthenticate { static public string VerifyUser( string username, string password) { 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; return "" ; return null ; } static public bool isAuthenticate() { return httpContext.Current.User.IDentity.IsAuthenticated; } static public voID SignOut() { FormsAuthentication.SignOut(); httpContext.Current.Session.Clear(); } } |
当点击登陆,用户名为xgr2004时就登陆,成功登陆后然后点 *** 作就会显示hello name的说明
反之如果没有登陆就显示notlogin
转载:http://www.cnblogs.com/Guroer/archive/2010/01/30/1660214.html
总结以上是内存溢出为你收集整理的Silverlight+WCF登录验证源代码下载全部内容,希望文章能够帮你解决Silverlight+WCF登录验证源代码下载所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)