小编sm1*_*101的帖子

ASP.NET Core Web App DI 错误 - 无法构建某些服务(验证服务描述符时出错

我正在创建一个 ASP.NET Core Web 应用程序。我正在通过库项目使用存储库。我在 Web 应用程序项目中引用了它。

存储库界面如下:

public interface IPushNotificationRepository
{
    IQueryable<PushNotification> Notifications
    {
        get;
    }
    IQueryable<Client> Clients
    {
        get;
    }

    void Add(PushNotification notification);
    void Add(Client client);
    void AddRange(IList<PushNotification> notifications);
    bool AddIfNotAlreadySent(PushNotification notification);
    void UpdateDelivery(PushNotification notification);
    bool CheckIfClientExists(string client);
    Client FindClient(int? id);
    void Update(Client client);
    void Delete(Client client);
}
Run Code Online (Sandbox Code Playgroud)

在存储库中,我注入了 db 上下文

    public class PushNotificationRepository : IPushNotificationRepository
    {
        private readonly PushNotificationsContext _context;

        public PushNotificationRepository(PushNotificationsContext context)
        {
            _context = context;
        }
}
Run Code Online (Sandbox Code Playgroud)

启动类的配置服务如下:

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();
    services.AddSingleton<IPushNotificationRepository, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc asp.net-core

5
推荐指数
1
解决办法
2万
查看次数

在 ASP.NET Core 3.1 中创建会话

我正在使用 ASP.NET Core 3.1。我需要在控制器的操作中设置会话。我在 ConfigureServices() 方法中添加了会话:

services.AddSession(options => {
                options.IdleTimeout = System.TimeSpan.FromSeconds(3600);
            });
Run Code Online (Sandbox Code Playgroud)

我还添加了在 Configure() 方法中使用 session 的代码:

app.UseSession();
Run Code Online (Sandbox Code Playgroud)

在控制器类中,我试图创建一个会话

HttpContext.Session.Set("sessionuser",xxx);
Run Code Online (Sandbox Code Playgroud)

所需的 xxx 是一个 byte[],与 .NET CORE 2.2 中 SetString() 所需的字符串不同,如何为此创建 byte[]?在创建会话中有其他更好的选择吗?

c# session .net-core asp.net-core

3
推荐指数
1
解决办法
2万
查看次数

如何在没有数据库的情况下在 ASP.NET Core 中创建一个简单的登录并授权控制器访问

我正在使用 ASP.NET Core 3.1。如何在不使用数据库的情况下创建一个简单的基于 ASP.NET Core 的登录。假设我没有使用数据库,而是在 appsettings.json 中有登录用户名和密码。我可以轻松访问并获取 appsettings 值。但是我应该如何实现登录功能以及我应该如何在 startup.cs 中配置服务(在配置和配置服务中)。

在 Configure() 方法中,我添加了 app.UseAuthentication();

当我登录并移动到使用注释 [Authorize] 的 Controller 类时,出现以下错误

处理请求时发生未处理的异常。InvalidOperationException: 未指定 authenticationScheme,也未找到 DefaultChallengeScheme。可以使用 AddAuthentication(string defaultScheme) 或 AddAuthentication(Action configureOptions) 设置默认方案。Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties) Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

c# asp.net-core

2
推荐指数
1
解决办法
4047
查看次数

标签 统计

asp.net-core ×3

c# ×3

.net-core ×1

asp.net-mvc ×1

session ×1