HTML5技术

在ASP.NET Core中使用Angular2,以及与Angular2的Token base身份认证 - 微软一站式示

字号+ 作者:H5之家 来源:H5之家 2016-11-20 15:00 我要评论( )

注:下载本文提到的完整代码示例请访问:How to authorization Angular 2 app with asp.net core web api 在ASP.NET Core中使用Angular2,以及与Angular2的Token base身份认证 Angular2是对Angular1的一次彻底的,破坏性的更新。 相对于Angular1.x,借用某果

注:下载本文提到的完整代码示例请访问:How to authorization Angular 2 app with asp.net core web api

在ASP.NET Core中使用Angular2,以及与Angular2的Token base身份认证

Angular2是对Angular1的一次彻底的,破坏性的更新。

相对于Angular1.x,借用某果的广告语,唯一的不同,就是处处都不同。

关于Angular2,强烈建议查阅官方文档:英文传送门 | 中文传送门

废话不多说,接下来的内容中,将介绍如何将 Angular2 整合到 ASP.NET Core 中,并实现一个Anguar2 和 ASP.NET Core Web API 的身份认证。

 

注意:本文章属于Step by step + Code Sample教程,且篇幅较长,建议下载本Sample并跟着本文进度自己重做一遍本例,下载完整代码并分析代码结构才有意义,下载地址:How to authorization Angular 2 app with asp.net core web api

1.前期准备 2.创建项目

在VS中新建项目,项目类型选择 ASP.NET Core Web Application(.Net Core),输入项目名称为:CSAuthorAngular2InASPNetCore,Template选择为Empty.

3.在项目中整合Angular2 3.1.配置Startup.cs

注:添加下面的代码时IDE会报代码错误,这是因为还没有引用对用的包,进入报错的这一行,点击灯泡,加载对应的包就可以了。

(图文无关)

在ConfigureServices中添加如下代码

services.AddMvc();

这里是添加MVC服务

在Configure中添加如下代码

app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: , template: ); });

第一句是启用静态文件,第二句是应用MVC模式并添加路由配置。

 

完整的代码应该是这个样子

public class Startup { // This method gets called by the runtime. Use this method to add services to the container. ConfigureServices(IServiceCollection services) { services.AddMvc(); } Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: , template: ); }); } }

3.2.添加控制器以及视图

3.2.1.在项目根目录下添加Controllers目录,并在其中添加一个控制器HomeController.cs,默认代码即可。

3.2.2.在项目跟目录下创建Views目录,在Views目录中新建目录Home, 最后在Home目录中新建视图Index.cshtml,内容应该是这样:

Angular QuickStart System.import((err){ console.error(err); }); Loading...

 

 现在运行项目的话你仅仅能看到一个Loading,再控制台中你还能看到错误,这是因为我们还没有配置Angular。让我们前往wwwroot目录。

3.3.在项目的wwwroot目录中添加如下结构:

3.3.1搭建Angular2基础环境

  • package.json

    { "name": "angular-quickstart", "version": "1.0.0", "scripts": { "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ", "lite": "lite-server", "postinstall": "typings install", "tsc": "tsc", "tsc:w": "tsc -w", "typings": "typings" }, "licenses": [ { "type": "MIT", "url": "https://github.com/angular/angular.io/blob/master/LICENSE" } ], "dependencies": { "@angular/common": "2.0.2", "@angular/compiler": "2.0.2", "@angular/core": "2.0.2", "@angular/forms": "2.0.2", "@angular/http": "2.0.2", "@angular/platform-browser": "2.0.2", "@angular/platform-browser-dynamic": "2.0.2", "@angular/router": "3.0.2", "@angular/upgrade": "2.0.2", "angular-in-memory-web-api": "0.1.5", "bootstrap": "3.3.7", "core-js": "2.4.1", "reflect-metadata": "0.1.8", "rxjs": "5.0.0-beta.12", "systemjs": "0.19.39", "zone.js": "0.6.25" }, "devDependencies": { "concurrently": "3.0.0", "gulp": "^3.9.1", "lite-server": "2.2.2", "typescript": "2.0.3", "typings": "1.4.0" } }

  • systemjs.config.js

    (function (global) { System.config({ paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { // our app is within the app folder app: 'app', // angular bundles '@angular/core': 'npm:@angular/core/bundles/core.umd.js', '@angular/common': 'npm:@angular/common/bundles/common.umd.js', '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js', '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js', '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js', '@angular/http': 'npm:@angular/http/bundles/http.umd.js', '@angular/router': 'npm:@angular/router/bundles/router.umd.js', '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js', '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js', // other libraries 'rxjs': 'npm:rxjs', 'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { app: { main: './main.js', defaultExtension: 'js' }, rxjs: { defaultExtension: 'js' } } }); })(this);

  • tsconfig.js

     

  • 1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

    相关文章
    • ASP.NET Aries 入门开发教程6:列表数据表格的格式化处理及行内编辑 - 路过秋天

      ASP.NET Aries 入门开发教程6:列表数据表格的格式化处理及行内编辑

      2016-11-20 16:00

    • 在ionic/cordova中使用百度地图插件 - LarryNE

      在ionic/cordova中使用百度地图插件 - LarryNE

      2016-11-20 13:00

    • ASP.NET Core 之 Identity 入门(二) - Savorboard

      ASP.NET Core 之 Identity 入门(二) - Savorboard

      2016-11-16 13:00

    • ASP.NET Core 之 Identity 入门(一) - Savorboard

      ASP.NET Core 之 Identity 入门(一) - Savorboard

      2016-11-07 15:00

    网友点评
    i