zoukankan      html  css  js  c++  java
  • 在ASP.NET Core 2.0中使用Facebook进行身份验证

      已经很久没有更新自己的技术博客了,自从上个月末来到天津之后把家安顿好,这个月月初开始找工作,由于以前是做.NET开发的,所以找的还是.NET工作,但是天津这边大多还是针对to B(企业)进行定制开发的,一是技术框架已经成型了,招收几个会CRUD的就好,二是小企业众多,给不了太多的钱,老板们大多比较关心这个程序员的工作年限,什么技术的不怎么关心。所以找工作一直碰壁,有看中的直接说我们给不了这么多,你如果要这个数可以考虑什么的。so,我就直接没考虑。一是钱少,二是技术垃圾,学不到什么东西。

      所以前几天开始直接转行了,找java工作,java工作由于自己没有工作经验,二是java技术经理有一种天生的优越感,说实话,很难受。好不容易找到一个有两个部门的公司,现在是负责对这个公司的.NET Core进行技术指导和Java部门进行CRUD。还行吧,先干着再说。

      我还是想说,我是一名.Net程序员吧,不论现在做那个方面吧,从我大学开始学.Net开始我是对.Net有感情的。我自身也一直看好.Net Core,也一直拥抱.Net Core的开源。将来也会一直支持.Net Core的发展。

    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    很多时候我们的系统框架需要集成单点登录来让用户使用第三方应用程序(如Facebook,Twitter,Google、QQ、微信等等)的账户登录我们的系统。在本文中,我将使用Facebook帐户来进行ASP.NET Core应用程序的身份验证。【如果Facebook访问404,请关闭此页面】

      1.安装.Net Core SDK(本文基于.Net Core 2.2)

      2.安装相关IDE(例如,VS、VS Code、JetBrains Rider)

    ⒉创建一个.Net Core MVC应用程序

    创建完成后目录如下

    ⒊使用数据库迁移更新数据库

      工具>> NuGet包管理器>>程序包管理器控制台。

    Update-Database

      迁移完成后可以通过SQL Server对象资源管理器查看自动为什么生成的数据库及表结构

    ⒋运行项目查看效果并记住URL

     

    创建一个Facebook应用程序

      1.访问facebook开发者网站并登录

      2.注册为开发者并新建一个应用

      

      3.选择应用场景

      

      4.点击左侧导航菜单中的Facebook登录>设置,在OAuth客户端授权设置页面的“ 有效OAuth跳转URI”字段中输入附加了/signin-facebook的应用程序的基本URL 。

      5.单击导航菜单上的设置>基本将看到我们刚刚创建的Facebook应用程序的应用编号(App ID)和应用密钥(App Secret)值。单击应用密钥字段内的显示按钮以查看密码。记住这两个值,我们将利用这两个值在我们的Web应用程序中配置Facebook身份验证。

    ⒍配置我们的应用程序以使用Facebook进行身份验证

      1.我们需要在应用程序中存储应用编号(App ID)和应用密钥(App Secret)字段值。我们将使用Secret Manager工具来实现此目的。Secret Manager工具是一个项目工具,可用于在开发过程中为.NET Core项目存储密码,API密钥等秘密。使用Secret Manager工具,我们可以将应用程序机密与特定项目相关联,并可以跨多个项目共享它们。

      在解决方案资源管理器中项目右键然后从上下文菜单中选择“管理用户机密”。

    将打开secrets.json文件,编写以下配置代码。

    1 {
    2   "Authentication:Facebook:AppId": "465373197596195",
    3   "Authentication:Facebook:AppSecret": "421e387a75dd8a3d96ab2ba5c8fb5329"
    4 }

      2.打开Startup.cs文件编写ConfigureServices 方法。

     1         public void ConfigureServices(IServiceCollection services)
     2         {
     3             services.Configure<CookiePolicyOptions>(options =>
     4             {
     5                 // This lambda determines whether user consent for non-essential cookies is needed for a given request.
     6                 options.CheckConsentNeeded = context => true;
     7                 options.MinimumSameSitePolicy = SameSiteMode.None;
     8             });
     9 
    10             services.AddDbContext<ApplicationDbContext>(options =>
    11                 options.UseSqlServer(
    12                     Configuration.GetConnectionString("DefaultConnection")));
    13             services.AddDefaultIdentity<IdentityUser>()
    14                 .AddDefaultUI(UIFramework.Bootstrap4)
    15                 .AddEntityFrameworkStores<ApplicationDbContext>();
    16 
    17             services.AddAuthentication().AddFacebook(facebookOptions =>
    18             {
    19                 facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
    20                 facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
    21             });
    22 
    23             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    24         }

      完整的Startup.cs文件

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Threading.Tasks;
     5 using Microsoft.AspNetCore.Builder;
     6 using Microsoft.AspNetCore.Identity;
     7 using Microsoft.AspNetCore.Identity.UI;
     8 using Microsoft.AspNetCore.Hosting;
     9 using Microsoft.AspNetCore.Http;
    10 using Microsoft.AspNetCore.HttpsPolicy;
    11 using Microsoft.AspNetCore.Mvc;
    12 using Microsoft.EntityFrameworkCore;
    13 using FbAuth.Data;
    14 using Microsoft.Extensions.Configuration;
    15 using Microsoft.Extensions.DependencyInjection;
    16 
    17 namespace FbAuth
    18 {
    19     public class Startup
    20     {
    21         public Startup(IConfiguration configuration)
    22         {
    23             Configuration = configuration;
    24         }
    25 
    26         public IConfiguration Configuration { get; }
    27 
    28         // This method gets called by the runtime. Use this method to add services to the container.
    29         public void ConfigureServices(IServiceCollection services)
    30         {
    31             services.Configure<CookiePolicyOptions>(options =>
    32             {
    33                 // This lambda determines whether user consent for non-essential cookies is needed for a given request.
    34                 options.CheckConsentNeeded = context => true;
    35                 options.MinimumSameSitePolicy = SameSiteMode.None;
    36             });
    37 
    38             services.AddDbContext<ApplicationDbContext>(options =>
    39                 options.UseSqlServer(
    40                     Configuration.GetConnectionString("DefaultConnection")));
    41             services.AddDefaultIdentity<IdentityUser>()
    42                 .AddDefaultUI(UIFramework.Bootstrap4)
    43                 .AddEntityFrameworkStores<ApplicationDbContext>();
    44 
    45             services.AddAuthentication().AddFacebook(facebookOptions =>
    46             {
    47                 facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
    48                 facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
    49             });
    50 
    51             services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
    52         }
    53 
    54         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    55         public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    56         {
    57             if (env.IsDevelopment())
    58             {
    59                 app.UseDeveloperExceptionPage();
    60                 app.UseDatabaseErrorPage();
    61             }
    62             else
    63             {
    64                 app.UseExceptionHandler("/Home/Error");
    65                 // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    66                 app.UseHsts();
    67             }
    68 
    69             app.UseHttpsRedirection();
    70             app.UseStaticFiles();
    71             app.UseCookiePolicy();
    72 
    73             app.UseAuthentication();
    74 
    75             app.UseMvc(routes =>
    76             {
    77                 routes.MapRoute(
    78                     name: "default",
    79                     template: "{controller=Home}/{action=Index}/{id?}");
    80             });
    81         }
    82     }
    83 }

    ⒎运行我们的应用程序进行测试

     

  • 相关阅读:
    Bootstrap--模仿官网写一个页面
    【ASP.NET基础】客户端、服务器端的数据验证 + CKEditer
    初识--Ajax & Json
    ASP.NET 状态的传递和保存
    启动项目报错org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'loadT
    Oracle数据库导不进去
    Tomcat一闪就退
    Oracle提示已连接到空闲的实例
    创建数据库表空间
    cmd命令导入.dmp文件
  • 原文地址:https://www.cnblogs.com/fanqisoft/p/11183143.html
Copyright © 2011-2022 走看看