Пример #1
0
        public async Task <IActionResult> Login([FromBody] LoginRequest request)
        {
            var user = await appUserManager.FindByNameAsync(request.Username);

            var pwd = await appUserManager.CheckPasswordAsync(user, request.Password);

            if (user != null && pwd)
            {
                Ulid ulid = Ulid.NewUlid();

                var authenticateClaims = new[]
                {
                    new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
                    new Claim(JwtRegisteredClaimNames.Jti, ulid.ToString())
                };

                var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("Make sure the secure key not short or it will throw a runtime exception of IDX10603 HS256 SecurityKey/KeySize"));

                var token = new JwtSecurityToken(
                    issuer: "https://www.slashand.com",
                    audience: "https://www.slashand.com",
                    expires: DateTime.UtcNow.AddDays(367),
                    claims: authenticateClaims,
                    signingCredentials: new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256)
                    );

                return(Ok(new
                {
                    token = new JwtSecurityTokenHandler().WriteToken(token),
                    expiration = token.ValidTo,
                }));
            }

            return(Unauthorized());
        }
Пример #2
0
        public static void Initialize(IServiceProvider serviceProvider)
        {
            var context     = serviceProvider.GetRequiredService <ApplicationDbContext>();
            var userManager = serviceProvider.GetRequiredService <UserManager <ApplicationUser> >();

            context.Database.EnsureCreated();

            if (!context.Users.Any())
            {
                Ulid ulid = Ulid.NewUlid();

                ApplicationUser user = new ApplicationUser()
                {
                    Id             = Ulid.NewUlid().ToString(),
                    Email          = "*****@*****.**",
                    EmailConfirmed = true,
                    FirstName      = "Thomas",
                    IsAdmin        = true,
                    LastName       = "Cayne",
                    SecurityStamp  = ulid.ToString(),
                    UserName       = "******",
                };

                userManager.CreateAsync(user, "Password@@Th0717#$@");
            }
        }
        /*
         *  Save folders are structured like this:
         *
         *  Saves/
         *      <Ulid of this save>/
         *          meta.bin                    - savefile metadata
         *          save.bin                    - actual savefile
         *      .../
         */

        string resolveSavePos(Ulid saveId)
        {
            var dataPos = Application.dataPath;

            return(Directory
                   .GetParent(dataPos)
                   .CreateSubdirectory("Saves")
                   .CreateSubdirectory(saveId.ToString())
                   .FullName);
        }
Пример #4
0
        public async Task <string> PutAttachment(Ulid id, string filename, Stream data, long size, string?contentType)
        {
            string key;
            var    filenameMatchResult = FilenameRegex.Match(filename);

            if (filenameMatchResult.Success)
            {
                key = string.Format(
                    "{0}/{1}_{2}.{3}",
                    AttachmentFolder,
                    filenameMatchResult.Groups["filename"].Value,
                    id,
                    filenameMatchResult.Groups["extension"].Value);
            }
            else
            {
                key = filename + "_" + id.ToString();
            }
            return(await PutObject(key, data, size, contentType));
        }
Пример #5
0
        // Note:
        // This validation uses a dynamic password sent to the user via email
        // instead of a regular password.
        public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
        {
            if (await db.CanUserLoginWithCode(context.UserName, context.Password))
            {
                // We have guarantee it will have value
                Ulid id = (await db.GetUserIdFromEmail(context.UserName)).Value;

                context.Result = new GrantValidationResult(
                    subject: id.ToString(),
                    authenticationMethod: "custom",
                    claims: new Claim[]
                {
                }
                    );
            }
            else
            {
                context.Result = new GrantValidationResult(
                    TokenRequestErrors.InvalidGrant,
                    "Bad passcode");
            }
        }
Пример #6
0
        public void New_ByteEquals_ToString_Equals()
        {
            for (int i = 0; i < 100; i++)
            {
                {
                    var ulid  = Ulid.NewUlid();
                    var nulid = new NUlid.Ulid(ulid.ToByteArray());

                    ulid.ToByteArray().Should().BeEquivalentTo(nulid.ToByteArray());
                    ulid.ToString().Should().Be(nulid.ToString());
                    ulid.Equals(ulid).Should().BeTrue();
                    ulid.Equals(Ulid.NewUlid()).Should().BeFalse();
                }
                {
                    var nulid = NUlid.Ulid.NewUlid();
                    var ulid  = new Ulid(nulid.ToByteArray());

                    ulid.ToByteArray().Should().BeEquivalentTo(nulid.ToByteArray());
                    ulid.ToString().Should().Be(nulid.ToString());
                    ulid.Equals(ulid).Should().BeTrue();
                    ulid.Equals(Ulid.NewUlid()).Should().BeFalse();
                }
            }
        }
Пример #7
0
        public void TestUlid()
        {
            // just create
            var anything = Ulid.Create();

            Assert.IsTrue(anything.ToString() != null);

            // from timestamp
            var dto  = DateTimeOffset.FromUnixTimeMilliseconds(1484581420);
            var ulid = Ulid.Create(dto);

            Assert.IsTrue(ulid.ToString().StartsWith("0001C7STHC"));

            // string roundtrip
            var str = anything.ToString();
            var rev = Ulid.Parse(str);

            Assert.IsTrue(str == rev.ToString());

            // bytes roundtrip
            var bytes = new byte[16];

            anything.AsBytes(bytes);
            var fromBytes = new Ulid(bytes);

            Assert.IsTrue(anything.Equals(fromBytes));
            Assert.IsTrue(anything.ToString() == fromBytes.ToString());

            // test operators
            var one   = new Ulid(100UL, 200UL);
            var two   = new Ulid(42UL, 43UL);
            var three = new Ulid(42UL, 43UL);

            Assert.IsTrue(one != two);
            Assert.IsTrue(two == three);
        }
Пример #8
0
 public string Ulid_()
 {
     return(ulid.ToString());
 }
Пример #9
0
        public void UlidCanConvertFromString()
        {
            var converted = _ulidConverter.ConvertFrom(_testUlid.ToString());

            Assert.Equal(_testUlid, converted);
        }