同样的像前面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. nfs服务端配置文件详解

    配置参数语法 配置文件路径 /etc/exports 可以使用#进行注释说明 一个完整的配置实例至少需要3个参数 1.被共享的目录 必须是存在的目录 2.向谁共享 可以指定主机或网络范围,支持通配符* ...

  2. java web中cookies的用法 转

    一.什么是cookies? 大家都知道,浏览器与WEB服务器之间是使用HTTP协议进行通信的,当某个用户发出页面请求时,WEB服务器只是简单的进行响应,然后就关闭与该用户的 连接.因此当一个请求发送到 ...

  3. java中的i++和++i区别

    public class Main { public static void main(String[] args) { int i = 0; i = i++; System.out.println( ...

  4. iOS关于友盟分享弹不出面板问题

    在程序代理类中声明 [NSThread sleepForTimeInterval:10];//设置启动页面时间 [self.window makeKeyAndVisible]; [[UMSocialM ...

  5. iOS 关于js与OC相互调用的那些事

    最近项目上使用js调用OC,OC再次调用JS,再次在JS页面上面回显数据. 项目中使用的是WKWebview,加载网路的URL,其实就是使用WK加载出来的H5网页,在项目中用的是H5网页有个识别按钮, ...

  6. 手机自动化测试:appium源码分析之bootstrap十四

    手机自动化测试:appium源码分析之bootstrap十四   poptest(www.poptest.cn)是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开 ...

  7. js在(FF)中长字段溢出(自动换行)

    function toBreakWord(el,intLen){    var obj=document.getElementByIdx_x(el);     var strContent=obj.i ...

  8. This Handler class should be static or leaks might occur(null) 解决办法 (转)

    原文链接:http://blog.csdn.net/wuleihenbang/article/details/17126371 首先解释下这句话This Handler class should be ...

  9. ASP.NET Core MVC和Visual Studio入门

    本教程将教你使用Visual Studio 2017创建 ASP.NET Core MVC web应用程序的基础知识. 安装Visual Studio 2017 和.Net Core 安装Visual ...

  10. POJ 2585 Window Pains 题解

    链接:http://poj.org/problem?id=2585 题意: 某个人有一个屏幕大小为4*4的电脑,他很喜欢打开窗口,他肯定打开9个窗口,每个窗口大小2*2.并且每个窗口肯定在固定的位置上 ...