欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.net MVC利用knockoutjs实现登陆并记录用户的内外网IP及所在城市(推荐)

程序员文章站 2023-11-13 21:08:10
前言 前面第一篇开了头个,现在想先从登陆写起,但感觉还有很多东西应该放在前面写,比如 1、mvc及web api的route配置,web api的route配置如何支持...

前言

前面第一篇开了头个,现在想先从登陆写起,但感觉还有很多东西应该放在前面写,比如

1、mvc及web api的route配置,web api的route配置如何支持命名空间

2、如何配置filters(实现安全验证、错误处理等等)

3、自定义filters、httprouteconstraint、modelbinder及httpparameterbinding等

这些问题在我开发过程中都有碰到,但感觉每一点都要说太多了。如果有需要到时候再回过头来写。

需求

还是老样子,我们先要明白要登陆实现哪些东西:

1、登陆页面(用户名、密码、记住我、登陆按钮、重置按钮)

2、消息显示(比如 错误时显示某某错误,登陆时显示正在登陆,登陆成功显示正在跳转等)

3、登陆处理(验证、登陆、正在登陆时禁用表单、更新用户登陆次数及时间、添加登陆履历其中要包括用户的内网ip外网ip还有所在城市、其它业务处理)

4、成功跳转

实现效果

在实现之前我们先看看实现出来的效果截图

登陆页面

Asp.net MVC利用knockoutjs实现登陆并记录用户的内外网IP及所在城市(推荐)

跳转页面

Asp.net MVC利用knockoutjs实现登陆并记录用户的内外网IP及所在城市(推荐)

登陆履历

Asp.net MVC利用knockoutjs实现登陆并记录用户的内外网IP及所在城市(推荐)

需求分析及实现

需求中基本都好实现,只有登陆履历中要记录内外网ip及所在城市要考虑一下。在asp.net中取得客户端内外网ip还是比较麻烦的,而要取得所在城市就基本不可能了,所以我们只好考虑借助第三方api去实现了。

1、内网ip直接在后台取

2、外网ip可以通过新浪api 取得,原来也可以返回城市的,后台不知道什么原因,只能返回ip了

3、所在城市通过百度api =取得,但是这个不会返回外网ip所以我就两个一起用了,挺蛋疼的。

以上在客户端去访问相应的api又存在一个跨域的问题,通过调查发现百度api支持jsonp,可以很好的解决跨域的问题,新浪api不支持但它返回一个变量,我们可以直接把新浪api写在页面srcipt中即可取得相应变量。

技术都应该没问题了,那我们开始写吧。

具体实现

第一步:在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 = "建筑材料管理系统";
 viewbag.enname = "engineering material mangange system";
 return view();
 }
 }
}

类要用allowanonymous属性修饰,才能保证未登陆也能够访问。

第二步:添加对应的view,添加~/views/login/index.cshtml,代码如下

@{
 viewbag.title = "登录系统";
 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>系统记住我</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信息,它返回的数据格式为

var ildata = new array("117.30.94.103","保留地址", "", "", ""); if (typeof(ildata_callback) != "undefined") { ildata_callback(); }

它其实也有一个callback函数,和jsonp类似,但函数名是固定的,并且没有传递数据。我们可以直接访问ildata[0]取得外网ip。

2、上面html中的data-bind=””写法为knouckoutjs的写法,用于绑定到viewmodel的属性

第三步:创建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的访问,其中加上了参数callback=?,jquery会自动处理成当前的回调函数,即跨域成功后会自动回调当前函数并传入数据。我们用viewmodel中的form.city接收请求的数据中的城市信息。

2、最后一句ko.applybindings(new viewmodel())即实现了页面和viewmodel的绑定,至此,前台全部完成。接下来写登陆处理doaction,还是放在logincontroller中,访问地址为/login/doaction。

第四步:在logincontroller中添加doaction的方法返回json数据。代码如下:

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); //更新用户登陆次数及时间
 this.appendloginhistory(request); //添加登陆履历
 mmsservice.loginhandler(request); //mms系统的其它的业务处理
 //返回登陆成功
 return new { status = "success", message = "登陆成功!" };
 }
 //更新用户登陆次数及时间
 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();
 }
 }
}

接收参数定义为jobject对象比较方便取得请求数据,数据服务中的getmodel是服务基类中已有的方法,这当中用到了两个函数,一个为updateuserlogincountanddate为更新用户登陆次数及时间的处理,另一个appendloginhistory添加登陆履历。至此已大功告成!

以上所述是小编给大家介绍的asp.net mvc利用knockoutjs实现登陆并记录用户的内外网ip及所在城市(推荐),希望对大家有所帮助