我有 asp.net core webapi 应用程序,当前在类中的方法中使用Microsoft.AspNetCore.Identityand ,到目前为止一切正常。services.AddDbContextConfigureServicesStartup
我试图用services.AddPooledDbContextFactory而不是来services.AddDbContext提高性能。但是,当我尝试使用时AddPooledDbContextFactory出现错误:
5[hostapp.Models.AppRole,hostapp.Data.DataContext,System.Int32,hostapp.Models.AppUserRole,Microsoft.AspNetCore.Identity.IdentityRoleClaimSystem.InvalidOperationException:尝试激活“Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore 1[System.Int32]]”时无法解析类型“hostapp.Data.DataContext”的服务
看到这个错误后,我创建了新的 webapi 项目,它使用AddPooledDbContextFactory没有Microsoft.AspNetCore.Identity它工作正常。所以我的问题是:
services.AddPooledDbContextFactory使用withMicrosoft.AspNetCore.Identity避免上述错误的正确方法是什么?
在ProjectRoot/Startup.cs
using System.Threading.Tasks;
using hostapp.Data;
using hostapp.GraphQL;
using hostapp.Interfaces;
using hostapp.Models;
using hostapp.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace hostapp
{
public class Startup
{
// ...
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<ICookieService, CookieService>(); …Run Code Online (Sandbox Code Playgroud) 我尝试使用 Gin 框架验证枚举在 Golang 中是否有效。
我遇到了这个解决方案:
但这种方法的缺点是硬编码值,每次枚举值更改时我们都必须手动更改该值。
有没有什么方法可以通过其名称作为字符串来获取枚举,而无需使用类型创建和注册映射?
package main
import "github.com/go-playground/validator"
type Status int
const (
Single Status = iota
Married
Other
)
type User struct {
Status Status `json:"status" binding:"Enum=Status"`
}
func Enum(fl validator.FieldLevel) bool {
enumType := fl.Param() // Status
// get `Status` by `enumType` and validate it...
return true
}
func main() {}
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写具有以下功能的简单 JWT 实现:
HMAC从头开始,以便更好地深入了解它是如何工作的。
到目前为止,我找到了这篇文章如何从头开始在 golang 中构建身份验证微服务。其中有一章致力于从头开始实现 JWT。我用它去生成令牌,但是当我将令牌粘贴到https://jwt.io时,我收到了无效签名和以下警告:
警告:您的 JWT 签名似乎未使用 base64url ( https://tools.ietforg/html/rfc4648#section-5 ) 正确编码。请注意,必须根据https://tools.ietf.org/html/rfc7515#section-2省略填充(“=”)
警告:您的 JWT 标头似乎未使用 base64url ( https://tools.ietforg/html/rfc4648#section-5 ) 正确编码。请注意,必须根据https://tools.ietf.org/html/rfc7515#section-2省略填充(“=”)
警告:您的 JWT 负载似乎未使用 base64url ( https://tools.ietforg/html/rfc4648#section-5 ) 正确编码。请注意,必须根据https://tools.ietf.org/html/rfc7515#section-2省略填充(“=”)
我粘贴的令牌如下所示:
eyAiYWxnIjogIkhTMjU2IiwgInR5cCI6ICJKV1QiIH0=.eyJhdWQiOiJmcm9udGVuZC5rbm93c2VhcmNoLm1sIiwiZXhwIjoiMTY1MTIyMjcyMyIsImlzcyI6Imtub3dzZWFyY2gubWwifQ==.SqCW8Hxakzck9Puzl0BEOkREPDyl38g2Fd4KFaDazV4=
我的 JWT 代码实现:
package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"time"
)
func GenerateToken(header string, payload map[string]string, secret string) (string, error) {
h := hmac.New(sha256.New, []byte(secret)) …Run Code Online (Sandbox Code Playgroud) 我想修改现有的通用存储库以添加可选select功能,例如 Entity Framework Core 中的功能。
期望的结果:
private readonly IUnitOfWork _unit;
// ...
// without using select functionality
IEnumerable<Entity> entities = await _unit.Account.AllAsync();
Entity? x = await _unit.Account.SingleAsync(x => x == id);
// using select functionality
IEnumerable<DTO> y = await _unit.Account.AllAsync(select: x => new DTO
{
Name = x.Name
});
DTO? y = await _unit.Account.SingleAsync(x => x == id, select: x => new DTO
{
Name = x.Name
});
Run Code Online (Sandbox Code Playgroud)
我尝试实现此问题的解决方案Select certain columns in a genericrepository function但参数是必需的,我希望它是可选的。
为简单起见,我只在通用存储库中保留要添加此功能的方法: …