tnblog
首页
视频
资源
登录

identityServer4 退出登录+EF

8395人阅读 2019/12/20 10:59 总访问:123663 评论:0 收藏:0 手机
分类: Net Core

登录讲完了 我们讲一下退出登录


退出比较简单啦

  1.  [HttpGet]
  2.         public async Task<IActionResult> Logout(string logoutId)
  3.         {
  4.             var logout = await _interaction.GetLogoutContextAsync(logoutId);
  5.             await HttpContext.SignOutAsync();
  6.             //获取客户端点击注销登录的地址
  7.             var refererUrl = Request.Headers["Referer"].ToString();
  8.              if (!string.IsNullOrWhiteSpace(refererUrl))
  9.             {
  10.                 return Redirect(refererUrl);
  11.             }
  12.             else
  13.             {
  14.                 //获取配置的默认的注销登录后的跳转地址
  15.                 if (logout.PostLogoutRedirectUri != null)
  16.                 {
  17.                     return Redirect(logout.PostLogoutRedirectUri);
  18.                 }
  19.             }
  20.             return View();

        }

我们在写一个方法就好 

  1.   public IActionResult logout()
  2.         {
  3.             return SignOut("Cookies""oidc");
  4.         }

下面我们讲一下 Net Core 使用EF


这里 我们电脑的版本可能一样 这里给几个下包

一.使用Nuget添加EF的依赖


输入命令:  Install-Package Microsoft.EntityFrameworkCore.SqlServer

安装成功后就可以在依赖项中看到

注意执行命令的项目你可能需要选择一下


 三.如果是使用db first,需要根据数据库生成model,就还需要使用命令添加两个依赖


Install-Package Microsoft.EntityFrameworkCore.Tools

Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design


安装成功后就可以在依赖项中看到



四.相关依赖添加成功后,就可以更具一个命令就可以从数据库生成model了  

 

命令:    Scaffold-DbContext "Server=.;Database=数据库名;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models


注意:有可能执行这个命令会报错:

 1:执行这一步的时候出现了点问题 ,因为系统是win7,powershell版本太低了,不支持这个命令,需要安装

3.0以上的powershell版本才行      


2: Could not load assembly 'DAL'. Ensure it is referenced by the startup project 'xxxx'.

是因为主项目没有添加到这个DAL层的引用,添加了就行了,所以估计执行这个命令会使用到启动项目的一些东西


3:Your startup project 'xxxxx' doesn't reference Microsoft.EntityFrameworkCore.Design.This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.

他是说你启动项目没有这个依赖,在启动项目里边执行一下这个两个命令就好了

Install-Package Microsoft.EntityFrameworkCore.Tools

Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design

好像执行执行那个.Tools也可以,我就奇怪了nuget执行的明明不是启动项目为什么启动项目中还要添加这个依赖呢,

只在启动项目添加这个依赖行不行呢


如果model已经生成过了,想全部覆盖的话,可以在后面加一个-force命令:

Scaffold-DbContext "Server=.;Database=Food;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -force


更新某个表:后面加-tables 表名

 Scaffold-DbContext "Server=.;Database=Food;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -tables Article

但是更新某个表有坑啊,如果覆盖了,那个表不会生成导航属性,而且那个山下文对象也只有那个表的内容了....暂时没有找到更好的办法...

单独更新拷贝过来,或者全部更新,或者直接写手吧,比如添加了一个字段什么的


     

 添加成功后在models可以看到, 生成了上下文对象与和表对应的model


官方文档

https://docs.microsoft.com/zh-cn/ef/core/miscellaneous/cli/powershell



然后就可以开始使用EF了

  1.  public IActionResult Index()        {             FoodContext fc = new FoodContext();             List<ProType> ptlist = fc.ProType.ToList();             ViewBag.ptlist = ptlist;             return View();        }



五.使用依赖注入来装载EF的上下文对象


 .net core中用了不少的依赖注入,官方文档中也推荐使用


1:删除方法     

  1.  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)        {            //#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.            optionsBuilder.UseSqlServer(@"Server=.;Database=Food;Trusted_Connection=True;");        }


2:添加方法 

  1.      public FoodContext(DbContextOptions<FoodContext> options)            : base(options)        {         }

添加的是一个构造函数用于构造函数注入(这个方法在新版的时候会自动加入)



3:在startup.cs的ConfigureServices方法中添加依赖注入    

  1.   public void ConfigureServices(IServiceCollection services)        {            // Add framework services.            services.AddMvc();             services.AddDbContext<FoodContext>(option => {                option.UseSqlServer("Data Source =.; Initial Catalog = EFCore_dbfirst; User ID = sa; Password = sa.123");            });                    }

注:usersqlserver是一个扩展方法,需要添加ef core的引用using Microsoft.EntityFrameworkCore;       


  • 连接字符串写入配置文件

      http://www.tnblog.net/aojiancc2/article/details/1266  



4:使用的时候就不能直接去实例化了否则会报错找不到上下文对象

应该使用注入的方式去获取ef对象,例如构造函数注入

  1.   private CNBlog_ServerContext ef;        public ArticleDAL(CNBlog_ServerContext context) //通过依赖注入得到实例        {            ef = context;        }




 微软官方文档:

 https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db 


下面就是我们的代码了

想要标准,高大上一点的话 我们就不直接new了

我们用依赖注入来做


一,先创建一个DAL

  1.   public Userinfo Loging(string name, string pwd)
  2.         {
  3.             
  4.                 Userinfo userinfo = _fakedbContext.Userinfo.Where(a => a.Name == name && a.Pwd == pwd).FirstOrDefault();
  5.                 return userinfo;
  6.         }

写标准一点 我们在这创2个文件夹

二,然后在Startup中配置好依赖注入关系

三,然后我们添加刚才配置好的依赖

  1.    //依赖注入
  2.         private readonly IIdentityServerInteractionService _interaction;
  3.         private readonly IUserDAL _userDAL;
  4.         public AccountController(IIdentityServerInteractionService interaction, IUserDAL userDAL)
  5.         {
  6.             _interaction = interaction;
  7.             _userDAL = userDAL;
  8.         }

最后一步我们就可以调用了

  1. [HttpPost]
  2.         public async Task<IActionResult> Login(string userName, string password, string returnUrl = null)
  3.         {
  4.             ViewData["returnUrl"] = returnUrl;
  5.             ViewBag.username1 = userName;
  6.             Userinfo userinfo=  _userDAL.Loging(userName, password);
  7.             if (userinfo != null)
  8.             {
  9.                 AuthenticationProperties props = new AuthenticationProperties
  10.                 {
  11.                     IsPersistent = true,
  12.                     ExpiresUtc = DateTimeOffset.UtcNow.Add(TimeSpan.FromDays(1)),
  13.                 };
  14.                 //注意这里应该引用Microsoft.AspNetCore.Http这个下面的
  15.                 await HttpContext.SignInAsync("10000", userName, props);
  16.                 //HttpContext.SignOutAsync();
  17.                 if (returnUrl != null)
  18.                 {
  19.                     return RedirectToLocal(returnUrl);
  20.                     //return Redirect("http://localhost:44396/home/index");
  21.                 }
  22.                 return View();
  23.             }
  24.             else
  25.             {
  26.                 return Content("登录失败");
  27.             }
  28.          }
  29.          
  30.      这样我们就搞定啦


评价

identityServer4退出登录跳转到原页面

IdentityServer4退出登录,注销登录已经封装好了我们使用其实很简单//注销登录 publicIActionResultLogout() { returnSig...

identityServer4 单点登陆

hello大家好又见面了今天给大家分享一下identityServer4 首选我们先来了解一下什么是identityServer. 什么是单点登录统一...

identityServer4 【实现单点登录】

今天记录一下 NET Core id4的单点登录虽然现在很流行 也很高大上但是第一次玩 还是很多的坑的。简单来讲就是一个项目登录了...

NET Core【identityServer4单点登录】+【退出登录

第一天接触NetCore,感觉坑很多,其他都还良好比如说我们现在有三个系统,一个是商品系统,一个是订单系统,另外一个就是单...

identityServer4携带自定义的Claim

identityServer4要携带自定义的Claim,仅仅传递Claim是不行的还需要实现IProfileService方法才行publicclassImplicitProfil...

identityServer4实现OAuth2.0四种模式之授权码模式

授权码模式隐藏码模式最大不同是授权码模式不直接返回token,而是先返回一个授权码,然后再根据这个授权码去请求token。这...

NET Core 使用 EF Code First

下面这些内容很老了看这篇:https://www.tnblog.net/aojiancc2/article/details/5365 项目使用多层,把数据库访问...

C out、rEF关键字的用法和区别

说说自己对out、ref的认识,面试问到的几率很高哟。out:classProgram { /* *out、ref都是引用传递,传递后使用都会改变...

Net Core使用EF之DB First

一.新建一个.net core的MVC项目新建好项目后,不能像以前一样直接在新建项中添加ef了,需要用命令在添加ef的依赖二.使用Nug...

EF6动态添加条件

例如我们要匹配一个集合中的所有关键字,我们首先想到的做法是这样的publicList&lt;Article&gt;GetArtByKeys(List&lt;strin...

Net Core使用依赖注入来装载EF的上下文对象

妹子情人节快乐~.net core中用了不少的依赖注入,官方文档中也推荐使用。这样使用依赖注入来管理ef对象,还是比较科学,比如...

NET CORE配置EF连接字符串。windows验证的连接字符串配置

在appsettings.json中配置好连接字符串{&quot;ConnectionStrings&quot;:{ &quot;BloggingDatabase&quot;:&quot;Server=(lo...

使用REFit框架访问REST接口

改装是一个类型安全的REST开源库,是一套基于RESTful架构的.NET客户端实现,内部使用HttpClient类封装,可通过改装更加简单...

docker启动报错 No dEFault Boot2Docker ISO found locally downloading the latest

这是因为,启动时如果检测到没有 Boot2Docker,就会去下载,这个下载过程出现网络连接上的错误了,导致启动失败。可以去下...

扩展EF自动映射需要查询的字段(表达式树Expression),动态构建返回值

Entity Framework 动态构造select表达式比如我们需要返回某些字段会采用如下的写法但是发现每次都去写select如果字段很多不...
最近老犯困
排名
29
文章
31
粉丝
12
评论
10
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术
幸福往往是摸得透彻,而敬业的心却常常隐藏。