本文介绍了我可以在 ASP.NET Core 启动期间访问数据库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在研究 .NET Core Web API.我刚刚按照 https://stormpath 上的指南尝试使用 JWT 进行身份验证.com/blog/token-authentication-asp-net-core.

I have recently been working on a .NET Core web API. I have just attempted authentication using JWT, by following the guide on https://stormpath.com/blog/token-authentication-asp-net-core.

一切都很顺利,直到我不得不用数据库查询替换 GetIdentity 方法中的硬编码用户名和密码,并意识到我不知道如何从这个文件中访问数据库!

All was going well until I had to replace the hard-coded username and passwords in the GetIdentity method with a DB query and realized I do not know how to access the DB from within this file!

我所指的方法显示在下面第 70 行的链接中.https://github.com/nbarbettini/SimpleTokenProvider/blob/master/test/SimpleTokenProvider.Test/Startup.Auth.cs

The method I am referring to is shown in the link below on line 70.https://github.com/nbarbettini/SimpleTokenProvider/blob/master/test/SimpleTokenProvider.Test/Startup.Auth.cs

我的问题如下.

  1. 我可以在这里访问数据库吗?如果是这样怎么办?
  2. 这应该是 GetIdentity 方法所在的位置,还是有更好的方法?

推荐答案

是的,可以访问数据库了!在 Configure 方法中运行的代码可以访问在 ConfigureServices 方法中添加的任何服务,包括数据库上下文等.

Yes, you can access the database! Code that runs in the Configure method can access any services that are added in the ConfigureServices method, including things like database contexts.

例如,如果您有一个简单的实体框架上下文:

For example, if you have a simple Entity Framework context:

using Microsoft.EntityFrameworkCore;
using SimpleTokenProvider.Test.Models;

namespace SimpleTokenProvider.Test
{
    public class SimpleContext : DbContext
    {
        public SimpleContext(DbContextOptions<SimpleContext> options)
            : base(options)
        {
        }

        public DbSet<User> Users { get; set; }
    }
}

然后将其添加到 ConfigureServices 中:

And you add it in ConfigureServices:

services.AddDbContext<SimpleContext>(opt => opt.UseInMemoryDatabase());

然后,在设置中间件的时候就可以访问了:

Then, you can access it when you are setting up the middleware:

var context = app.ApplicationServices.GetService<SimpleContext>();

app.UseSimpleTokenProvider(new TokenProviderOptions
{
    Path = "/api/token",
    Audience = "ExampleAudience",
    Issuer = "ExampleIssuer",
    SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
    IdentityResolver = (username, password) => GetIdentity(context, username, password)
});

并稍微重写一下 GetIdentity 方法:

And rewrite the GetIdentity method a little:

private Task<ClaimsIdentity> GetIdentity(SimpleContext context, string username, string password)
{
    // Access the database using the context
    // Here you'd need to do things like hash the password
    // and do a lookup to see if the user + password hash exists
}

我是原始样本的作者.抱歉一开始不清楚!我尝试以一种易于提供您自己的功能的方式编写 IdentityResolver 委托——例如与您自己的数据库集成(如上),或将其连接到 ASP.NET Core Identity.当然,您也可以随意丢弃我的代码并做得更好.:)

I'm the author of the original sample. Sorry it wasn't clear initially! I tried to write the IdentityResolver delegate in a way that makes it easy to provide your own functionality -- like integrating with your own database (as above), or hooking it up to ASP.NET Core Identity. Of course, you're free to throw away my code and do something better, too. :)

这篇关于我可以在 ASP.NET Core 启动期间访问数据库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:33
查看更多