Asp.net MVC利用knockoutjs實現(xiàn)登陸并記錄用戶的內外網IP及所在城市(推薦)
前言
前面第一篇開了頭個,現(xiàn)在想先從登陸寫起,但感覺還有很多東西應該放在前面寫,比如
1、MVC及Web API的Route配置,Web API的Route配置如何支持命名空間
2、如何配置Filters(實現(xiàn)安全驗證、錯誤處理等等)
3、自定義Filters、HttpRouteConstraint、ModelBinder及HttpParameterBinding等
這些問題在我開發(fā)過程中都有碰到,但感覺每一點都要說太多了。如果有需要到時候再回過頭來寫。
需求
還是老樣子,我們先要明白要登陸實現(xiàn)哪些東西:
1、登陸頁面(用戶名、密碼、記住我、登陸按鈕、重置按鈕)
2、消息顯示(比如 錯誤時顯示某某錯誤,登陸時顯示正在登陸,登陸成功顯示正在跳轉等)
3、登陸處理(驗證、登陸、正在登陸時禁用表單、更新用戶登陸次數(shù)及時間、添加登陸履歷其中要包括用戶的內網IP外網IP還有所在城市、其它業(yè)務處理)
4、成功跳轉
實現(xiàn)效果
在實現(xiàn)之前我們先看看實現(xiàn)出來的效果截圖
登陸頁面
跳轉頁面
登陸履歷
需求分析及實現(xiàn)
需求中基本都好實現(xiàn),只有登陸履歷中要記錄內外網IP及所在城市要考慮一下。在asp.NET中取得客戶端內外網IP還是比較麻煩的,而要取得所在城市就基本不可能了,所以我們只好考慮借助第三方api去實現(xiàn)了。
1、內網IP直接在后臺取
2、外網IP可以通過新浪API http://counter.sina.com.cn/ip 取得,原來也可以返回城市的,后臺不知道什么原因,只能返回IP了
3、所在城市通過百度API http://api.map.baidu.com/location/ip?ak=&ip=取得,但是這個不會返回外網IP所以我就兩個一起用了,挺蛋疼的。
以上在客戶端去訪問相應的API又存在一個跨域的問題,通過調查發(fā)現(xiàn)百度API支持JSONP,可以很好的解決跨域的問題,新浪API不支持但它返回一個變量,我們可以直接把新浪API寫在頁面srcipt中即可取得相應變量。
技術都應該沒問題了,那我們開始寫吧。
具體實現(xiàn)
第一步:在MVC中新建LoginController添加如下代碼
using System; using System.Web.Mvc; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Zephyr.Core; using Zephyr.Models; using Zephyr.Web.Areas.Mms.Common; namespace Zephyr.Controllers { [AllowAnonymous] public class LoginController : Controller { public ActionResult Index() { ViewBag.CnName = "建筑材料管理系統(tǒng)"; ViewBag.EnName = "Engineering Material Mangange System"; return View(); } } }
類要用AllowAnonymous屬性修飾,才能保證未登陸也能夠訪問。
第二步:添加對應的View,添加~/Views/Login/Index.cshtml,代碼如下
@{ ViewBag.Title = "登錄系統(tǒng)"; Layout = null; } <!doctype html> <html> <head> <title>@ViewBag.Title</title> <link href="~/Content/css/page/login.css" rel="stylesheet" type="text/css" /> <script src="~/Content/js/jquery/jquery-1.8.1.min.js"></script> <script src="~/Content/js/core/json2.js"></script> <script src="~/Content/js/core/knockout-2.2.1.js"></script> <script src="~/Content/js/viewModel/login.js"></script> <script src="http://counter.sina.com.cn/ip"></script> </head> <body> <div class="second_body"> <form data-bind="submit:loginClick"> <div class="logo"><img src="/Content/images/login/logo.png" alt="" /></div> <div class="title-zh">@ViewBag.CnName</div> <div class="title-en" style="@ViewBag.EnNameStyle">@ViewBag.EnName</div> <div class="message" data-bind="html:message"></div> <table border="0" style="width:300px;"> <tr> <td style="padding-bottom: 5px;width:55px;">用戶名:</td> <td colspan="2"><input type="text" class="login" data-bind="value:form.usercode" /></td> </tr> <tr> <td class="lable" style="letter-spacing: 0.5em; vertical-align: middle">密碼:</td> <td colspan="2"><input type="password" class="login" data-bind="value:form.password" /></td> </tr> <tr> <td></td> <td colspan="2"><input type="checkbox" data-bind="checked:form.remember" /><span>系統(tǒng)記住我</span></td> </tr> <tr> <td colspan="3" style="text-align:center"> <input type="submit" value="登錄" class="login_button" /> <input type="button" value="重置" class="reset_botton" data-bind="click:resetClick" /> </td> </tr> </table> </form> </div> </body> </html>
1、腳本的最后一個即添加新浪API獲取外網IP信息,它返回的數(shù)據(jù)格式為
var ILData = new Array("117.30.94.103","保留地址", "", "", ""); if (typeof(ILData_callback) != "undefined") { ILData_callback(); }
它其實也有一個callback函數(shù),和JSONP類似,但函數(shù)名是固定的,并且沒有傳遞數(shù)據(jù)。我們可以直接訪問ILData[0]取得外網IP。
2、上面html中的data-bind=””寫法為knouckoutjs的寫法,用于綁定到viewModel的屬性
第三步:創(chuàng)建ViewModel
var viewModel = function () { var self = this; this.form = { usercode: ko.observable(), password: ko.observable(), remember:ko.observable(false), ip: null, city: null }; this.message = ko.observable(); this.loginClick = function (form) { $.ajax({ type: "POST", url: "/login/doAction", data: ko.toJSON(self.form), dataType: "json", contentType: "application/json", success: function (d) { if (d.status == 'success') { self.message("登陸成功正在跳轉,請稍候..."); window.location.href = '/'; } else { self.message(d.message); } }, error: function (e) { self.message(e.responseText); }, beforeSend: function () { $(form).find("input").attr("disabled", true); self.message("正在登陸處理,請稍候..."); }, complete: function () { $(form).find("input").attr("disabled", false); } }); }; this.resetClick = function () { self.form.usercode(""); self.form.password(""); self.form.remember(false); }; this.init = function () { self.form.ip = ILData[0]; $.getJSON("http://api.map.baidu.com/location/ip?ak=F454f8a5efe5e577997931cc01de3974&callback=?", function (d) { self.form.city = d.content.address; }); if (top != window) top.window.location = window.location; }; this.init(); }; $(function () { ko.applyBindings(new viewModel());});
定義viewModel,其屬性包括from表單信息,message提示信息,loginClick登陸,resetClick重置。其中的init部分其實可以不放到viewModel中。
1、$.getJSON即為JSONP的訪問,其中加上了參數(shù)callback=?,jQuery會自動處理成當前的回調函數(shù),即跨域成功后會自動回調當前函數(shù)并傳入數(shù)據(jù)。我們用viewModel中的form.city接收請求的數(shù)據(jù)中的城市信息。
2、最后一句ko.applyBindings(new viewModel())即實現(xiàn)了頁面和viewModel的綁定,至此,前臺全部完成。接下來寫登陸處理doAction,還是放在LoginController中,訪問地址為/login/doAction。
第四步:在LoginController中添加doAction的方法返回JSON數(shù)據(jù)。代碼如下:
public JsonResult DoAction(JObject request) { var message = new sys_userService().Login(request); return Json(message, JsonRequestBehavior.DenyGet); }
然后在service層中處理
using System; using System.Collections.Generic; using Zephyr.Core; using System.Dynamic; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using Zephyr.Utils; using Zephyr.Web.Areas.Mms.Common; namespace Zephyr.Models { public class sys_userService : ServiceBase<sys_user> { public object Login(JObject request) { var UserCode = request.Value<string>("usercode"); var Password = request.Value<string>("password"); //用戶名密碼檢查 if (String.IsNullOrEmpty(UserCode) || String.IsNullOrEmpty(Password)) return new { status = "error", message = "用戶名或密碼不能為空!" }; //用戶名密碼驗證 var result = this.GetModel(ParamQuery.Instance() .AndWhere("UserCode", UserCode) .AndWhere("Password", Password) .AndWhere("IsEnable", true)); if (result == null || String.IsNullOrEmpty(result.UserCode)) return new { status = "error", message = "用戶名或密碼不正確!" }; //調用框架中的登陸機制 var loginer = new LoginerBase { UserCode = result.UserCode, UserName = result.UserName }; FormsAuth.SignIn(loginer.UserCode, loginer, 60 * 8); //登陸后處理 this.UpdateUserLoginCountAndDate(UserCode); //更新用戶登陸次數(shù)及時間 this.AppendLoginHistory(request); //添加登陸履歷 MmsService.LoginHandler(request); //MMS系統(tǒng)的其它的業(yè)務處理 //返回登陸成功 return new { status = "success", message = "登陸成功!" }; } //更新用戶登陸次數(shù)及時間 public void UpdateUserLoginCountAndDate(string UserCode) { db.Sql(@" update sys_user set LoginCount = isnull(LoginCount,0) + 1 ,LastLoginDate = getdate() where UserCode = @0 " , UserCode).Execute(); } //添加登陸履歷 public void AppendLoginHistory(JObject request) { var lanIP = ZHttp.ClientIP; var hostName = ZHttp.IsLanIP(lanIP) ? ZHttp.ClientHostName : string.Empty; //如果是內網就獲取,否則出錯獲取不到,且影響效率 var UserCode = request.Value<string>("usercode"); var UserName = MmsHelper.GetUserName(); var IP = request.Value<string>("ip"); var City = request.Value<string>("city"); if (IP != lanIP) IP = string.Format("{0}/{1}", IP, lanIP).Trim('/').Replace("::1", "localhost"); var item = new sys_loginHistory(); item.UserCode = UserCode; item.UserName = UserName; item.HostName = hostName; item.HostIP = IP; item.LoginCity = City; item.LoginDate = DateTime.Now; db.Insert<sys_loginHistory>("sys_loginHistory", item).AutoMap(x => x.ID).Execute(); } } }
接收參數(shù)定義為JObject對象比較方便取得請求數(shù)據(jù),數(shù)據(jù)服務中的GetModel是服務基類中已有的方法,這當中用到了兩個函數(shù),一個為UpdateUserLoginCountAndDate為更新用戶登陸次數(shù)及時間的處理,另一個AppendLoginHistory添加登陸履歷。至此已大功告成!
以上所述是小編給大家介紹的Asp.net MVC利用knockoutjs實現(xiàn)登陸并記錄用戶的內外網IP及所在城市(推薦),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
- knockoutjs模板實現(xiàn)樹形結構列表
- 使用asp.net mvc,boostrap及knockout.js開發(fā)微信自定義菜單編輯工具(推薦)
- KnockoutJS 3.X API 第四章之表單textInput、hasFocus、checked綁定
- KnockoutJS 3.X API 第四章之數(shù)據(jù)控制流foreach綁定
- KnockoutJS 3.X API 第四章之數(shù)據(jù)控制流with綁定
- KnockoutJS 3.X API 第四章之事件event綁定
- KnockoutJS 3.X API 第四章之表單value綁定
- BootstrapTable+KnockoutJS相結合實現(xiàn)增刪改查解決方案(三)兩個Viewmodel搞定增刪改查
- KnockoutJS數(shù)組比較算法實例詳解
相關文章
JavaScript用JQuery呼叫Server端方法實現(xiàn)代碼與參考語法
從Javascript客戶端用JQuery呼叫Server端的方法,這也是一個大膽的嘗試,本人做了演示動畫以及參考語法,感興趣的朋友可以參考下,希望本人對你有所幫助2013-01-01asp.net獲取當前網址url的各種屬性(文件名、參數(shù)、域名 等)的代碼
用 asp.net獲取當前網頁地址的一些信息。2010-07-07asp.net+jquery ajax無刷新登錄的實現(xiàn)方法
asp.net+jquery ajax無刷新登錄的實現(xiàn)方法,需要的朋友可以參考一下2013-06-06