简体   繁体   English

如何访问 OnSignedIn .net Core - 实体框架中的数据库数据

[英]How to access the database data in OnSignedIn .net Core - Entity framework

When a user signs in to a .net core MVC program, I need to view user-specific data from the database using logged in user id and save those data to current cookie.当用户登录到 .net 核心 MVC 程序时,我需要使用登录的用户 ID 从数据库中查看特定于用户的数据,并将这些数据保存到当前 cookie。 Following is the path I'm now taking.以下是我现在走的路。 Show me how to do this if something is wrong.如果出现问题,请告诉我如何执行此操作。

is there any way access like有什么办法可以访问吗

dbcontext.footable.where(x=> x.id = "somevalue")

Startup.cs启动.cs

services.ConfigureIdentitySettings(apiSetting);

ConfigureIdentitySettings.cs配置身份设置.cs

public static class IdentitySettingsExtensions
    {
        // Identity Setup
        public static void ConfigureIdentitySettings(this IServiceCollection services, IConfigurationSection apiSetting)
        {
            services.ConfigureApplicationCookie(options =>
            {
                options.Cookie.Name = "foo";
                options.ExpireTimeSpan = TimeSpan.FromMinutes(Configurations.ExipireTimeSpan);
                options.ReturnUrlParameter = CookieAuthenticationDefaults.ReturnUrlParameter;
                options.LoginPath = Configurations.LoginPath;
                options.LogoutPath = Configurations.LogoutPath;
                options.AccessDeniedPath = Configurations.AccessDeniedPath;
                options.SlidingExpiration = true;
                options.Events = new CookieAuthenticationEvents
                {
                    OnSignedIn = context =>
                    {
                        // Here i got current logged in user id
                        var loggedInUserRole = context.Principal.GetLoggedInUserId();

                        //I need to access database data from here after that adding those data into current cookie
                        return Task.CompletedTask;
                    }                    
                };
            });

            services.Configure<IdentityOptions>(options =>
            {
                // Providers
                //options.Tokens.PasswordResetTokenProvider = PasswordResetTokenProviderName;
                //options.Tokens.EmailConfirmationTokenProvider = ConfirmEmailTokenProviderName;
                options.SignIn.RequireConfirmedEmail = true;
                options.User.RequireUniqueEmail = true;              

                options.Password.RequireUppercase = Configurations.RequireUppercase;
                options.Password.RequireLowercase = Configurations.RequireLowercase;
                options.Password.RequireDigit = Configurations.RequireDigit;
                options.Password.RequiredLength = Configurations.RequireLength;
                options.Password.RequireNonAlphanumeric = Configurations.RequireNonAlphanumeric;
            });

            var firstLifeSpan = Convert.ToInt32(apiSetting["FirstEmailConfirmationLifeSpan"]);
            var secondLifeSpan = Convert.ToInt32(apiSetting["SecondEmailConfirmationLifeSpan"]);
            services.Configure<DataProtectionTokenProviderOptions>(o =>
                o.TokenLifespan = TimeSpan.FromDays(firstLifeSpan > secondLifeSpan ? firstLifeSpan : secondLifeSpan));

        }
    }

You can access database data in OnSignedIn method like below:您可以在OnSignedIn方法中访问数据库数据,如下所示:

services.ConfigureApplicationCookie(options =>
{
    //...
    options.Events = new CookieAuthenticationEvents
    {
        OnSignedIn = context =>
        {

            //Build an intermediate service provider
            var sp = services.BuildServiceProvider();

            //Resolve the services from the service provider
            var myDbContext = sp.GetService<ApplicationDbContext>();

            //access database data...
            var data = myDbContext.footable.Where(x => x.Id== "xxx");

            //...
            return Task.CompletedTask;
        }
    };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用实体框架在 .NET Core 6 中播种数据? - How to seed data in .NET Core 6 with Entity Framework? 使用 ASP.NET Core 和 Entity Framework Core 进行集成测试 - 如何在每个测试中恢复数据库中的测试数据? - Integration Testing with ASP.NET Core and Entity Framework Core - How to Restore Test Data in Database Per Test? 如何使用 Entity Framework .NET Core 将数据插入到我的数据库中? - How do I insert data into my database using Entity Framework .NET Core? 实体框架核心-如何访问Context.Database.Migrate() - Entity Framework Core - How To Access Context.Database.Migrate() 如何在.NET Core中撤消Entity Framework Update-Database - How to undo Entity Framework Update-Database in .NET Core 如何使用 Net core 3 和 Entity Framework 由新客户端创建新数据库? - How to create new database by new client with Net core 3 and Entity Framework? .NET Core Entity Framework - 异步写入数据库 - .NET Core Entity Framework - asynchronous writting into database 实体框架数据库 First.Net Core - Entity Framework Database First .Net Core 数据库优先错误实体框架.NET Core - Database First Error Entity Framework .NET Core 如何使用 Entity Framework 在 .NET Core 中保存用户数据 - How to save user data in .NET Core with Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM