public async Task <IActionResult> Register([FromBody] RegisterViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = new VanguardUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { await _signInManager.SignInAsync(user, isPersistent : false); // TODO: Audit logging return(Ok()); } AddErrors(result); return(BadRequest(ModelState)); }
private async Task <AuthenticationTicket> CreateTicketAsync( OpenIdConnectRequest request, VanguardUser user, AuthenticationProperties properties = null) { var principal = await _signInManager.CreateUserPrincipalAsync(user); var ticket = new AuthenticationTicket(principal, properties, OpenIddictServerDefaults.AuthenticationScheme); if (!request.IsRefreshTokenGrantType()) { ticket.SetScopes(new[] { OpenIdConnectConstants.Scopes.OpenId, OpenIdConnectConstants.Scopes.Email, OpenIdConnectConstants.Scopes.Profile, OpenIdConnectConstants.Scopes.OfflineAccess, OpenIddictConstants.Scopes.Roles }.Intersect(request.GetScopes())); } ticket.SetResources("vanguard-identity-management"); foreach (var claim in ticket.Principal.Claims) { if (claim.Type == _identityOptions.Value.ClaimsIdentity.SecurityStampClaimType) { continue; } var destinations = new List <string> { OpenIdConnectConstants.Destinations.AccessToken }; if (claim.Type == OpenIdConnectConstants.Claims.Name && ticket.HasScope(OpenIdConnectConstants.Scopes.Profile) || claim.Type == OpenIdConnectConstants.Claims.Email && ticket.HasScope(OpenIdConnectConstants.Scopes.Email) || claim.Type == OpenIdConnectConstants.Claims.Role && ticket.HasScope(OpenIddictConstants.Claims.Roles)) { destinations.Add(OpenIdConnectConstants.Destinations.IdentityToken); } claim.SetDestinations(destinations); } return(ticket); }
public async Task <IActionResult> Register([FromBody] RegisterViewModel model) { if (ModelState.IsValid) { var user = new VanguardUser { UserName = model.UserName, Email = model.UserName }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { return(Ok()); } AddErrors(result); } // If we got this far, something failed. return(BadRequest(ModelState)); }
public async Task <IActionResult> RegisterNode([FromBody] ServerNodeViewModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var username = Guid.NewGuid().ToString(); var password = StringGenerator.GetRandomString(24); var user = new VanguardUser { UserName = username }; var userResult = await _userManager.CreateAsync(user, password); if (!userResult.Succeeded) { ModelState.AddIdentityErrors(userResult.Errors); return(BadRequest(ModelState)); } await _userManager.AddToRoleAsync(user, RoleConstants.NodeAgent); var createResult = await _service.CreateAsync(model, user); if (!createResult.Succeeded) { ModelState.AddEntityTransactionErrors(createResult.Errors); await _userManager.DeleteAsync(user); return(BadRequest(ModelState)); } return(CreatedAtRoute("GetServerNode", new { id = createResult.Value.Id }, new UsernamePasswordCredentialsViewModel { Username = username, Password = password })); }
public async Task <EntityTransactionResult <ServerNodeViewModel> > CreateAsync(ServerNodeViewModel model, VanguardUser user, CancellationToken cancellationToken = default) { if (await AnyAsync(t => t.Name == model.Name, cancellationToken)) { return(EntityTransactionResult <ServerNodeViewModel> .Failure(EntityTransactionError.CreateUniqueError("Name", model.Name))); } var entity = new ServerNode { Name = model.Name, PublicKey = model.PublicKey, User = user }; var result = await _context.ServerNodes.AddAsync(entity, cancellationToken); if (await _context.SaveChangesAsync(cancellationToken) == 0) { return(EntityTransactionResult <ServerNodeViewModel> .Failure(EntityTransactionError.CreateNoResultsError())); } return(EntityTransactionResult <ServerNodeViewModel> .Success(await ToViewModelAsync(result.Entity, cancellationToken))); }
public static void Main(string[] args) { var webHost = CreateWebHostBuilder(args).Build(); var serviceScope = webHost.Services.CreateScope(); var app = new CommandLineApplication { Name = "servermanager-core" }; app.HelpOption("-?|-h|--help", true); app.Command("start", command => { command.OnExecute(() => webHost.Run()); }); app.Command("migrate", command => { command.Description = "Apply database migrations"; command.OnExecute(async() => { var dbContext = serviceScope.ServiceProvider.GetService <VanguardDbContext>(); await dbContext.Database.MigrateAsync(); }); }); app.Command("createsuperuser", command => { command.Description = "Create a new super user to the system"; var emailArgument = command.Argument("email", "UserName address for the new super user."); var providedPasswordOption = command.Option("-s|--set-password", "Set user password via argument.", CommandOptionType.SingleValue); command.OnExecute(async() => { // TODO: Audit logging var userName = string.IsNullOrEmpty(emailArgument.Value) ? Prompt.GetString("Provide the user email address:") : emailArgument.Value; var user = new VanguardUser { UserName = userName, Email = userName }; string password; if (providedPasswordOption.HasValue()) { password = providedPasswordOption.Value(); } else { password = Prompt.GetPassword("Provide the user password:"******"Confirm the user password:"******"Passwords don't match"); } } var userManager = serviceScope.ServiceProvider.GetService <UserManager <VanguardUser> >(); var result = await userManager.CreateAsync(user, password); if (!result.Succeeded) { Console.WriteLine(string.Join('\n', result.Errors.Select(t => $"[{t.Code}] {t.Description}"))); return(1); } foreach (var fieldInfo in typeof(RoleConstants).GetFields()) { result = await userManager.AddToRoleAsync(user, fieldInfo.Name); if (!result.Succeeded) { Console.WriteLine(string.Join('\n', result.Errors.Select(t => $"[{t.Code}] {t.Description}"))); return(1); } } return(0); }); if (args.Length == 0) { app.ShowHint(); } else { app.Execute(args); } }); }