在控制台.NET Core应用程序中创建用户

编程入门 行业动态 更新时间:2024-10-10 15:21:37
本文介绍了在控制台.NET Core应用程序中创建用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个包含3个项目(Web,控制台应用程序,DataAccessLayer)的ASP.NET Core 1.0解决方案. 我使用ASP.NET Core身份和Entity Framework Core(SQL Server-代码优先).

I have a ASP.NET Core 1.0 Solution with 3 projects (Web, Console Application, DataAccessLayer). I use ASP.NET Core Identity and Entity Framework Core (SQL Server - Code First).

我想在控制台应用程序(用于后台任务)中创建用户,但是如何在控制台应用程序(或.NET Core类库)中访问UserManager对象?

In my Console Application (Used for background tasks), I want to create users, but how I can have access to UserManager object in a Console Application (Or in a .NET Core Class Library) ?

在控制器类中,使用依赖注入很容易:

In a controller class, it's easy with Dependency Injection :

public class AccountController : Controller { private readonly UserManager<ApplicationUser> _userManager; public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager) { _userManager = userManager; } //... [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<IActionResult> Register(RegisterViewModel model) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); //... }

如何在Console Core Application中完成等效的工作?

How I can do the equivalent in a Console Core Application ?

推荐答案

感谢Tseng的回答,我最终获得了这段代码.以防万一有人需要:

Thanks to Tseng's answer I ended up with this code. Just in case if someone would need:

public class Program { private interface IUserCreationService { Task CreateUser(); } public static void Main(string[] args) { var services = new ServiceCollection(); services.AddDbContext<ApplicationDbContext>( options => { options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=my-app-db;Trusted_Connection=True;MultipleActiveResultSets=true"); }); // Authentification services.AddIdentity<ApplicationUser, IdentityRole>(opt => { // Configure identity options opt.Password.RequireDigit = false; opt.Password.RequireLowercase = false; opt.Password.RequireUppercase = false; opt.Password.RequireNonAlphanumeric = false; opt.Password.RequiredLength = 6; opt.User.RequireUniqueEmail = true; }) .AddEntityFrameworkStores<ApplicationDbContext>() .AddDefaultTokenProviders(); services.AddScoped<IUserCreationService, UserCreationService>(); // Build the IoC from the service collection var provider = services.BuildServiceProvider(); var userService = provider.GetService<IUserCreationService>(); userService.CreateUser().GetAwaiter().GetResult(); Console.ReadKey(); } private class UserCreationService : IUserCreationService { private readonly UserManager<ApplicationUser> userManager; public UserCreationService(UserManager<ApplicationUser> userManager) { this.userManager = userManager; } public async Task CreateUser() { var user = new ApplicationUser { UserName = "TestUser", Email = "test@example" }; var result = await this.userManager.CreateAsync(user, "123456"); if (result.Succeeded == false) { foreach (var error in result.Errors) { Console.WriteLine(error.Description); } } else { Console.WriteLine("Done."); } } } }

更多推荐

在控制台.NET Core应用程序中创建用户

本文发布于:2023-11-05 02:42:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559691.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制台   应用程序   用户   Core   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!