在asp.net 应用程序和WCF服务之间共享FormsAuthentication,默认是不支持的,设置一下非常的简单,只需要两步就可以了: 1、在web.config的system.serviceModel formsConfig.Timeout.TotalMinutes), // 失效时间 true,""); // 用户数据 //加密票 string encryptedTicket = FormsAuthentication.Encrypt (formAuthTicket); // 以加密票的密文存入Cookie HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName ; authCookie.Secure = FormsAuthentication.RequireSSL; if (FormsAuthentication.CookieDomain = null) { authCookie.Domain = FormsAuthentication.CookieDomain;
) { ticket = FormsAuthentication.RenewTicketIfOld(tOld); ticket.IsPersistent, ticket.UserData, "/"); } string cookieValue = FormsAuthentication.Encrypt = null) { cookie = e.Context.Request.Cookies[FormsAuthentication.FormsCookieName ; cookie.HttpOnly = true; if (FormsAuthentication.CookieDomain = null) { cookie.Domain = FormsAuthentication.CookieDomain
如果用户验证成功,你只需要告诉ASP.NET架构验证成功(通过调用FormsAuthentication类的一个方法),运行库会自动设置验证cookie(实际上包含了票据)并将用户转到原先请求的页面。 private void CreateToken(Models.User user) { string json = JsonConvert.SerializeObject(user); FormsAuthentication.SetAuthCookie (user.UserName.Trim(), true, FormsAuthentication.FormsCookiePath); FormsAuthenticationTicket Ticket , FormsAuthentication.Encrypt(Ticket)); } 其中Biz126.Cache.SetCookies.AddCookie是我自己封装的保存Cookie方法。 ];//获取cookie FormsAuthenticationTicket Ticket = FormsAuthentication.Decrypt(authCookie.Value
public void SetCookie(string userName, string role,string cookieValueName) {FormsAuthentication.FormsCookieName userName, System.DateTime.Now.AddMinutes(30),DateTime.Now, false, role); string encryptTick=FormsAuthentication.Encrypt HttpContext.Current.Response.Cookies.Add(myCookie); } 这是我登陆后设置cookie的方法,本来cookieValueName是用FormsAuthentication.FormsCookieName
Encoding.Default.GetBytes(Text); des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile inputByteArray[x]=(byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile sKey, "md5").Substring(0, 8)); des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile
domain, string lDAP, string userName, string password, string permissionCode, bool persistCookie, bool formsAuthentication 权限编号</param>
/// <param name="persistCookie">是否保存密码</param>
/// <param name="<em>formsAuthentication</em> domain, string lDAP, string userName, string password, string permissionCode, bool persistCookie, bool <em>formsAuthentication</em> RemoveUserCookie();<br/> }<br/> LogOn(userInfo, <em>formsAuthentication</em>
= null) { FormsAuthentication.RedirectFromLoginPage(model.UserName , false); } else { FormsAuthentication.SetAuthCookie 第四步:对于登出的Action进行处理如下: public ActionResult LogOff() { FormsAuthentication.SignOut } } </script> 此时关闭的情况调用的Action如下: public void ClosePage() { FormsAuthentication.SignOut
Upper32(string s) { var hashPasswordForStoringInConfigFile = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile Lower32(string s) { var hashPasswordForStoringInConfigFile = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile Upper16(string s) { var hashPasswordForStoringInConfigFile = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile Lower16(string s) { var hashPasswordForStoringInConfigFile = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile
对了,不信把Cookie禁用掉(建议用最BT的办法,把Documents and Settings\Administrator\Cookies目录设置为任何用户都无权读取),然后就登录不了系统 3.除了FormsAuthentication.SignOut 从2中很容易想到,只要能找到特定用户的客户端Cookie,并使其过期就可以了,看下面的代码: HttpCookie _cookie = FormsAuthentication.GetAuthCookie
.net core 是非对称加密,所以要在 .net core 中动手的话必须要对 .net core 默认的加密和解密操作进行拦截,如果可行的话最好的方案应该是将 .net framework 的 FormsAuthentication ,剪不断理还乱,github 找到其开源地址:https://github.com/Microsoft/referencesource/blob/master/System.Web/Security/FormsAuthentication.cs Route("getMyTicket")] public IHttpActionResult GetMyTicket(string cookie) { var formsAuthTicket = FormsAuthentication.Decrypt RedirectToAction("Index"); } webapi 获取 cookie [Route("getMyCookie")] public string GetMyCookie(string name) { FormsAuthentication.SetAuthCookie (name, false); return FormsAuthentication.GetAuthCookie(name, false).Value; } 其余代码不用做任何更改,ok,我们来测试一下
是的,这套系统使用了微软FormsAuthentication类作为身份认证。 FormsAuthentication类提供了一个 Encrypt 方法,用于创建一个字符串值,该字符串值可以存储在 cookie 中,也可以存储在 URL 中 FormsAuthenticationTicket FormsAuthentication类还提供了一 个Decrypt 方法,用于 FormsAuthenticationTicket 根据从 forms 身份验证 cookie 或 URL 检索到的加密的身份验证票证来创建对象
Newtonsoft.Json.JsonConvert.SerializeObject(admin)); //数据加密 string enyTicket = FormsAuthentication.Encrypt AdminLoginInfo mode = null; try { FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt
当SetAuthCookie()或RedirectFromLoginPage()被调用时FormsAuthentication类自动创建认证Cookie。 DateTime.Now.AddMinutes(30), // value of time out property false, // Value of IsPersistent property String.Empty, FormsAuthentication.FormsCookiePath ); 然后就可以使用FormsAuthentication类的Encrypt方法来加密ticket。 string encryptedTicket = FormsAuthentication.Encrypt(ticket); Q85.
WebService,则可能被恶意用户利用,造成性能以及安全性的问题,所以我们需要使用一些验证方式来保护WebService,最常见方式就是Forms Authentication,这也是一种用法很简单的方式 一个使用FormsAuthentication ("xiaoyaojian", false);//使用FormsAuthentication,登陆Username为xiaoyaojian的用户,false表示不使用持久化的Cookie } protected void btnLogout_Click(object sender, EventArgs e) { FormsAuthentication.SignOut Service的功能就是为我们提供一种以AJAX方式登陆和注销用户的功能,如果我们自己来实现,就会使用客户端调用WebService来实现这样的功能,而它给我们提供了更多的,比如跳转之类,实际上他的内部还是通过FormsAuthentication ,点击Login,弹出成功的提示,但是我们点击GetRandom,是不会出现随机数的,因为我们没有执行FormsAuthentication.setAuthCookie,这也正好证明了我们扩展的AuthenticationService
的登录 User.Identity.IsAuthenticated 将设置为True // User.Identity.Name 会设置成我们下面的UserName FormsAuthentication.SetAuthCookie = null) { //注册完成之后直接登录用户 FormsAuthentication.SetAuthCookie return View(model); } 登出相关代码 [HttpPost] [ValidateAntiForgeryToken] public ActionResult LogOff() { FormsAuthentication.SignOut Index", "Home"); } 修改密码相关代码 [HttpPost] [ValidateAntiForgeryToken] public ActionResult LogOff() { FormsAuthentication.SignOut
Upper32(string s) { var hashPasswordForStoringInConfigFile = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile Lower32(string s) { var hashPasswordForStoringInConfigFile = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile Upper16(string s) { var hashPasswordForStoringInConfigFile = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile Lower16(string s) { var hashPasswordForStoringInConfigFile = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile
FormsAuthentication.SetAuthCookie是必须写的吗? 是必须写的。让我们了解一些小的工作细节。 客户端通过浏览器给服务器发送请求。 FormsAuthentication.SetAuthCookie将添加 “Authentication”特殊的Cookie来响应。 是否意味着没有Cookies,FormsAuthentication 将不会有作用? 不是的,可以使用URI代替Cookie。 FormsAuthentication.SetAuthCookie中第二个参数”false“表示什么? false决定了是否创建永久有用的Cookie。 AuthenticationController 添加新的Logout action方法: 1: public ActionResult Logout() 2: { 3: FormsAuthentication.SignOut
; FormsAuthentication.SignOut();//注销用户 } else { this.lblMessage.Text ; FormsAuthentication.SetAuthCookie(this.txtUsername.Text, false);//使用户登陆 } } e) { } protected void btnLogin_Click(object sender, EventArgs e) { FormsAuthentication.SetAuthCookie this.txtUserName.Text, false); } protected void btnLogout_Click(object sender, EventArgs e) { FormsAuthentication.SignOut
puser.Email = TBEmail.Text; puser.UserCode = TBCode.Text; puser.UserPassword = FormsAuthentication.HashPasswordForStoringInConfigFile
Newtonsoft.Json.JsonConvert.SerializeObject(new {name="test"})); string ticString = FormsAuthentication.Encrypt