登录成功之后跳转回原先载入的链接

1、需登录之后才能查看的页面,首先判断是否登录,未登录时去到登录页面,参数formUrl的值是登录前要去到的链接

1 if(!IsUserLogin()){
2         string hostAndPort = HttpContext.Current.Request.Url.AbsoluteUri.ToLower();
3         if (!string.IsNullOrEmpty(HttpContext.Current.Request.Url.Query))
4             hostAndPort = hostAndPort.Replace(HttpContext.Current.Request.Url.Query.ToLower(), "");
5         if (!string.IsNullOrEmpty(HttpContext.Current.Request.Url.LocalPath))
6             hostAndPort = hostAndPort.Replace(HttpContext.Current.Request.Url.LocalPath.ToLower(), "");
7         Response.Redirect("/zh_cn/login.html?fromUrl=" + Server.UrlEncode(hostAndPort + HttpContext.Current.Request.RawUrl));
8         Response.End();
9 }

2、登录页面脚本

 1 $(function() {
 2     $('.sub').click(function() {
 3         var str = '';
 4         var index = document.URL.indexOf("fromUrl=");
 5         if (index == -1) {
 6             var fromUrl = "";
 7         } else {
 8             var fromUrl = document.URL.slice(index + 8);
 9         }
10         if ($('#username').val() == '') {
11             str += '用户名不能为空
';
12         }
13         if ($('#password').val() == '') {
14             str += '密码不能为空
';
15         }
16         if (str.length > 0) {
17             alert(str);
18             return false;
19         } else {
20             $.post('/tools/submit_ajax.ashx?action=user_login&site={site.build_path}&fromUrl=' + fromUrl, $('#form_login').serialize(), function(data) {
21                 if (data.status == 1) {
22                     if (data.url != "") {
23                         location.href = data.url;
24                     } else {
25                         location.href = "/zh_cn/index.html";
26                     }
27                 } else {
28                     alert(data.info);
29                 }
30             }, 'json')
31             return false;
32         }
33     })
34 })

 3、一般处理程序

1 string fromUrl = STRequest.GetQueryString("fromUrl");
2 context.Response.Write("{"status":1, "info":"会员登录成功!","url":"" + fromUrl + ""}");
原文地址:https://www.cnblogs.com/jronny/p/5140767.html