Ejemplo n.º 1
0
        public static void AddMRMongoIdentity
        <TUser, TUserStore, TUserManager>(this IServiceCollection services, MRDatabaseConnectionSettings settings, MRTokenSettings tokenSettings, Action <IdentityOptions> userSignupActions = null)
            where TUser : MRUser, new()
            where TUserStore : MRUserStore <TUser>
            where TUserManager : MRUserManager <TUser>
        {
            services.AddSingleton(settings);
            services.AddSingleton(tokenSettings);

            services.AddTransient <IHttpContextAccessor, HttpContextAccessor>();

            services.AddTransient <IMRUserStore <TUser>, TUserStore>();
            services.AddTransient <IMRRoleStore, MRRoleStore>();
            services.AddTransient <IUserValidator <TUser>, MRUserValidator <TUser> >();

            services.AddTransient <MRRoleManager>();
            services.AddTransient <TUserManager>();
            services.AddTransient <MRSignInManager <TUser> >();
            services.AddSingleton <MRTokenManager <TUser> >();

            services.AddSingleton <MRTokenManager>();

            services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options =>
            {
                options.RequireHttpsMetadata      = tokenSettings.RequireHttps;
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = tokenSettings.ValidateIssuer,
                    ValidIssuer              = tokenSettings.Issuer,
                    ValidateAudience         = tokenSettings.ValidateAudience,
                    ValidAudience            = tokenSettings.Audience,
                    ValidateLifetime         = tokenSettings.ValidateLifetime,
                    IssuerSigningKey         = MRTokenSettings.GetSymmetricSecurityKey(tokenSettings.Key),
                    ValidateIssuerSigningKey = tokenSettings.ValidateSigningKey,
                };
            });

            userSignupActions = userSignupActions ?? new Action <IdentityOptions>((a) => {
                a.User.RequireUniqueEmail = true;
            });

            services.AddIdentityCore <TUser>(userSignupActions)
            .AddDefaultTokenProviders();
        }
Ejemplo n.º 2
0
        public virtual Tuple <string, DateTime> Generate(TUser user, IEnumerable <string> roles)
        {
            var identity = GetIdentity(user, roles);

            var now     = DateTime.UtcNow;
            var expires = now.Add(TimeSpan.FromSeconds(_settings.Lifetime));

            var jwt = new JwtSecurityToken(
                issuer: _settings.Issuer,
                audience: _settings.Audience,
                notBefore: now,
                expires: expires,
                claims: identity.Claims,
                signingCredentials: new SigningCredentials(MRTokenSettings.GetSymmetricSecurityKey(_settings.Key), SecurityAlgorithms.HmacSha256));

            return(new Tuple <string, DateTime>(new JwtSecurityTokenHandler().WriteToken(jwt), expires));
        }