我正在创建一个 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) 我正在使用 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[]?在创建会话中有其他更好的选择吗?
我正在使用 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)