Skip to content

Instantly share code, notes, and snippets.

@danielpetisme
Last active June 11, 2019 19:52
Show Gist options
  • Save danielpetisme/26214b89d34460848e1e8065c471f325 to your computer and use it in GitHub Desktop.
Save danielpetisme/26214b89d34460848e1e8065c471f325 to your computer and use it in GitHub Desktop.
dotnet demos
using System;
using System.Collections.Generic;
using System.Linq;
public class UserDTO {
public string Id { get; set; }
public string Login { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string ImageUrl { get; set; }
public bool Activated { get; set; }
public string LangKey { get; set; }
public ISet<string> Roles { get; set; }
}
public class Program
{
public static void Main()
{
var userDto = new UserDTO {
Login = "johwick",
FirstName = "John",
LastName = "Wick",
Email = "john@iluvmydog.com",
Activated = false,
ImageUrl = "http://placehold.it/50x50",
LangKey = "en",
Roles = new HashSet<string> {
"ROLE_USER"
}
};
Console.WriteLine(userDto.FirstName);
}
}
public static class WebConfiguration {
public static IServiceCollection AddWebModule(this IServiceCollection @this)
{
@this.AddHttpContextAccessor();
//https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-2.2
@this.AddHealthChecks();
@this.AddMvc(options => {
options.ModelBinderProviders.Insert(0, new PageableBinderProvider());
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(options => {
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
});
return @this;
}
}
public async Task<User> CreateUser(UserDto userDto)
{
var user = new User {
UserName = userDto.Login.ToLower(),
FirstName = userDto.FirstName,
LastName = userDto.LastName,
Email = userDto.Email.ToLower(),
ImageUrl = userDto.ImageUrl,
LangKey = userDto.LangKey ?? Constants.DefaultLangKey,
PasswordHash = "..."
ResetKey = RandomUtil.GenerateResetKey(),
ResetDate = DateTime.Now,
Activated = true
};
await _userManager.CreateAsync(user);
await CreateUserRoles(user, userDto.Roles);
_log.LogDebug($"Created Information for User: {user}");
return user;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment