Silverlight+WCF登录验证源代码下载
来源:互联网 发布:青海干部教育网络平台 编辑:程序博客网 时间:2024/06/08 19:34
对于简单的安全性不高的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”操作有详细的说明
代码的相关介绍:
网上查阅了相关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 - Windows集成验证
- 喷泉Silverlight源代码下载
- silverlight + silverlight-enable wcf 问题
- WCF RIA 登录验证+cookie之完整登录验证加保留用户状态
- Silverlight 2.0 Beta 控件源代码和SDK文档下载
- Silverlight 实时图表生成程序 ChartBuilder 源代码下载
- Silverlight新闻轮刷动画控件源代码下载
- Silverlight 博客整站源代码+数据库(完整版下载)
- Silverlight的登录页面中经常用到输入验证码
- 如何用silverlight 做用于图片展示的画廊(三):利用silverlight + WCF +Linq 保存图片或照片,并利用silverlight +WCF下载图片显示在画廊中
- Silverlight + WCF 架构问题
- Silverlight WCF 上传实现
- Silverlight调用WCF服务
- silverlight+wcf 错误
- 开发WCF/Silverlight须知
- Silverlight + WCF 数据压缩
- WCF/Silverlight注意事项
- JAVA程序类加载及其反射机制
- [ASP.NET MVC]让Html.RenderAction支持Lamda表达式
- 假如这个世界少了我,多我一个不多少我一个不少:QQ伤感日志
- 曲面分类
- 语音编码原理
- Silverlight+WCF登录验证源代码下载
- 有意思的互联网创业公司(Timehop/Redbeacon )
- struts2 注解方式注册action中 namespace 的注意事项
- hdu4424
- 发送邮件触发器
- #ifdef,#else,#endif,#if用法详解(转)
- 关于效率
- test
- ORTP 简介