同样的像前面2篇博文一样,每个项目的开始基本都是建个空的Web项目

在NuGet库中安装以下几个NuGet包

  • Nancy
  • Nancy.Authentication.Forms
  • Nancy.Hosting.Aspnet
  • Nancy.ViewEngines.Razor

项目中同样分别建Module,Models,Views 三个文件夹,并在Module文件夹里面添加MainModule类

 public MainModule()
{
Get("/", Lexan => { return View["index"]; });
Get("/login", Lexan =>
{
dynamic model = new ExpandoObject();
model.Errored = this.Request.Query.error.HasValue;
return View["login",model];
});
}

接着往Models文件夹里面添加UserDatabase类

private static List<Tuple<string, string, Guid>> user = new List<Tuple<string, string, Guid>>();
static UserDatabase()
{
//guid码是一个唯一的id,这个id可以自己另外写一个guid生成器去生成
//登陆账号Lexan,密码是password
user.Add(new Tuple<string, string, Guid>("Lexan","password",new Guid("87e516f4-c759-430d-a552-18ad8e65483b")));
}
public ClaimsPrincipal GetUserFromIdentifier(Guid identifier,NancyContext context)
{
//guid值赋给userRecord
var userRecord = user.FirstOrDefault(x=>x.Item3==identifier);
return userRecord == null ? null : new ClaimsPrincipal(new GenericIdentity(userRecord.Item1));
}
public static Guid? ValidateUser(string username,string password)
{
var usersRecord = user.FirstOrDefault(x=>x.Item1==username&&x.Item2==password);
if (usersRecord==null)
{
return null;
}
return usersRecord.Item3;
}

接着继续回到MainModule文件夹里面去写完类里面的post登陆方法和get登出方法

 Post("/login",Lexan=>
{
var userGuid = UserDatabase.ValidateUser((string)this.Request.Form.Username,(string)this.Request.Form.Password);
if (userGuid==null)
{
return this.Context.GetRedirect("~/login?error=true&username="+(string)this.Request.Form.Username);
}
DateTime? expiry = null;
if (this.Request.Form.RememberMe.HasValue)
{
expiry = DateTime.Now.AddDays();
}
return this.LoginAndRedirect(userGuid.Value,expiry);
});
//登出,并返回到主页
Get("/logout",Lexan=> { return this.LogoutAndRedirect("~/"); });

继续往Module文件夹里面添加PartlySecureModule类,SecureModule类,以及Models文件里面添加UserModel类

 public PartlySecureModule():base("/partlysecure")
{
Get("/",Lexan=>"");
Get("/secured",Lexan=>
{
this.RequiresAuthentication();
//获取UserModel里面的Name值
var model = new UserModel(this.Context.CurrentUser.Identity.Name);
return View["secure.cshtml",model];
});
}

SecureModule类

  public SecureModule():base("/secure")
{
this.RequiresAuthentication();
Get("/",Lexan=>
{
var model = new UserModel(this.Context.CurrentUser.Identity.Name);
return View["secure.cshtml",model];
});
}

UserModel类

        public string Username { get; set; }
public UserModel(string username)
{
Username = username;
}

还有一个引导项目的FormsBootstrapper类,添加在根目录

        protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
// base.ConfigureApplicationContainer(container);
}
protected override void ConfigureRequestContainer(TinyIoCContainer container, NancyContext context)
{
base.ConfigureRequestContainer(container, context);
//这里我们将用户映射器注册为每个请求的单一实例。
//由于现在是每个请求, 我们可以注入一个请求范围
//数据库 "上下文" 或其他请求范围的服务。
container.Register<IUserMapper,UserDatabase>();
}
protected override void RequestStartup(TinyIoCContainer container, IPipelines pipelines, NancyContext context)
{
//base.RequestStartup(container, pipelines, context); //在请求启动时, 我们修改请求管线
//包括 forms 身份验证-在我们现在的请求中传递
//作用域的用户名映射程序。
//在这里传递的管道是特定于此请求的,
//因此, 我们可以添加 / 删除 / 更新的项目, 如下所示。
var formsAuthenConfiguration = new FormsAuthenticationConfiguration()
{
RedirectUrl="~/login",
UserMapper=container.Resolve<IUserMapper>()
};
FormsAuthentication.Enable(pipelines,formsAuthenConfiguration);
}

以上的后台逻辑是基本的写完了,接下里处理界面,在View是文件夹里面添加index,login,secure页面

页面就暂时这么处理,运行看看FormsDemo

最后的提醒别忘记里修改Web.config哦

这里声明一下,本来昨天博主要更新的,但是由于某些事情,没能及时更新

NancyFx 2.0的开源框架的使用-Forms的更多相关文章

  1. NancyFx 2.0的开源框架的使用-Basic

    这是NancyFx开源框架中的Basic认证,学习一下! 首先当然是新建一个空的Web,BasicDemo 继续在项目中添加Nuget包,记得安装的Nuget包是最新的预发行版 Nancy Nancy ...

  2. NancyFx 2.0的开源框架的使用-CustomModule(自定义模块)

    NancyFx框架的自定义模块 新建一个空的Web项目 然后通过NuGet库安装下面的包 Nancy Nancy.Hosting.Aspnet 然后添加Models,Module,Views三个文件夹 ...

  3. NancyFx 2.0的开源框架的使用-ModelBinding(实现绑定)

    NancyFx框架中使用绑定模型 新建一个空的Web程序 然后安装Nuget库里面的包 Nancy Nancy.Hosting.Aspnet Nancy.ViewEnglines.Spark 并在We ...

  4. NancyFx 2.0的开源框架的使用-HosingOwin

    Nancy框架的Owin使用 先建一个空的Web项目 然后往Nuget库里面添加Nancy包 Nancy Nancy.Owin Nancy.ViewEnglines.Spark 然后添加Models, ...

  5. NancyFx 2.0的开源框架的使用-Authentication

    新建一个空的项目 新建好了空的项目以后,接着通过NuGet安装一下三个包 Nancy Nancy.Hosting.Aspnet Nancy.ViewEnglines.Razor 然后在项目中添加Mod ...

  6. NancyFx 2.0的开源框架的使用-Stateless

    同样和前面一样新建一个空的Web项目,都在根目录添加Module,Models,Views文件夹 添加Nuget包 在Models文件夹里面添加UserModel类 public string Use ...

  7. NancyFx 2.0的开源框架的使用-Stateless(二)

    继续上一篇Stateless的博文,在上一篇的博文的基础上稍微加点东西 接下来右键解决方案添加新项目,一样建一个空的Web项目 然后在StatelessDemoWeb项目里面添加Views文件夹,Sc ...

  8. NancyFx 2.0的开源框架的使用-AspnetBootstrapping

    新建一个空的Web项目AspnetBootstrappingDemo 然后添加NuGet组件 Nancy Nancy.Hosting.Aspnet Nancy.ViewEngines.Razor 继续 ...

  9. NancyFx 2.0的开源框架的使用-Caching

    新建一个空的Web项目,命名CachingDemo 然后添加三个Nuget安装包 Nancy Nancy.Hosting.Aspnet Nancy.ViewsEngines.Razor 然后往项目里面 ...

随机推荐

  1. Sublime Text3 编辑器我的最爱

    简介 Sublime Text 3是一个神奇的文本编辑器,适合程序员.作家.它有很多亮点功能,比如多选择.Go Anything.命令面板.多选择可以让你同时编辑多出代码,Got Anything 像 ...

  2. [SinGuLaRiTy] 2017-03-30 综合性测试

    [SinGuLaRiTy-1014] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 对于所有的题目:Time Limit:1s  |  Me ...

  3. 手机自动化测试:appium问题解决

    手机自动化测试:appium问题解决   Appium遇到问题: 问题一:问题org.openqa.selenium.remote.UnreachableBrowserException: Could ...

  4. Eclipse插件ObjectAid(UML画图工具)

    原链接:https://my.oschina.net/keyven/blog/856594 最近想找一个Eclipse上的插件,可以方便的把java代码转换为UML图,但是由于我用的是Eclipse ...

  5. 解决eclipse中文字很小

    新下载的eclipse4.2.1版本,显示中文字体很小,但是英文比较正常.网上查看要更改字体大小,但是更改后英文也变大了,不是想要的结果. window – preferences – general ...

  6. ngrok把本地主机映射到公网域名

    这两天又要搞微信项目,然后我下载了一个QQ浏览器,搜索微信调试工具,我再搜,再搜,搜不出来,问了下客服,暂时下架了,好吧! 我上网搜了一下,就找到了  ngrok 这个东西,它也可以把你本地主机映射到 ...

  7. ASP.NET Core 网站发布到Linux服务器

    长期以来,使用.NET开发的应用只能运行在Windows平台上面,而目前国内蓬勃发展的互联网公司由于成本的考虑,大量使用免费的Linux平台,这就使得.NET空有一身绝技但无法得到广大的施展空间,.N ...

  8. mysql 主从同步 实现增量备份

    数据库复制 replication 的实现原理 1:主服务器凡运行语句,都产生一个二进制日志 binlog 2:从服务器不断读取主服务器的binlog 3:从主服务读取到的binlog,转换为自身可执 ...

  9. day002-HTML知识点总结:浏览器兼容性之指定IE浏览器使用chrome内核渲染页面

    今天再浏览大淘宝首页时,突然看到这么一个东东: ,顿时好费解,莫非万恶的IE浏览器认识到自己以往的罪孽,开始兼容chrome了??! 于是本着不懂就百度的神精,开始纵横于各大铁耙,勃哥,终于找到了许许 ...

  10. SVG格式转Visio的vsd格式方法,附带C#动态调用Office的Com组件方法

    SVG格式可以直接显示在网页上面,用来实现诸如统计Chart图表.流程图.组织结构图的功能.为了使图像可以下载下来以便于修改,可以将SVG转为Visio的vsd格式.方法很简单,主要是使用Visio组 ...