示例#1
0
        private SmsAuthenticationOptions GetOptions()
        {
            var result = new SmsAuthenticationOptions();

            if (_properties != null)
            {
                var message    = string.Empty;
                var accountSid = string.Empty;
                var authToken  = string.Empty;
                var fromNumber = string.Empty;
                if (_properties.TryGetValue("Message", out message))
                {
                    result.Message = message;
                }

                if (_properties.TryGetValue("AccountSid", out accountSid))
                {
                    result.TwilioSmsCredentials.AccountSid = accountSid;
                }

                if (_properties.TryGetValue("AuthToken", out authToken))
                {
                    result.TwilioSmsCredentials.AuthToken = authToken;
                }

                if (_properties.TryGetValue("FromNumber", out fromNumber))
                {
                    result.TwilioSmsCredentials.FromNumber = fromNumber;
                }

                result.ClaimsIncludedInUserCreation = _properties.TryGetArr("ClaimsIncludedInUserCreation");
            }

            return(result);
        }
        public static IServiceCollection AddSmsAuthentication(this IServiceCollection services, IMvcBuilder mvcBuilder, SmsAuthenticationOptions smsAuthenticationOptions)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            if (mvcBuilder == null)
            {
                throw new ArgumentNullException(nameof(mvcBuilder));
            }

            if (smsAuthenticationOptions == null)
            {
                throw new ArgumentNullException(nameof(smsAuthenticationOptions));
            }

            var assembly             = typeof(AuthenticateController).Assembly;
            var embeddedFileProvider = new EmbeddedFileProvider(assembly);

            services.Configure <RazorViewEngineOptions>(opts =>
            {
                opts.FileProviders.Add(embeddedFileProvider);
                opts.CompilationCallback = (context) =>
                {
                    var assm            = MetadataReference.CreateFromFile(Assembly.Load("SimpleIdentityServer.Authenticate.Basic").Location);
                    context.Compilation = context.Compilation.AddReferences(assm);
                };
            });
            services.AddSingleton(smsAuthenticationOptions);
            services.AddTransient <ITwilioClient, TwilioClient>();
            services.AddTransient <ISmsAuthenticationOperation, SmsAuthenticationOperation>();
            services.AddTransient <IGenerateAndSendSmsCodeOperation, GenerateAndSendSmsCodeOperation>();
            services.AddTransient <IAuthenticateResourceOwnerService, SmsAuthenticateResourceOwnerService>();
            mvcBuilder.AddApplicationPart(assembly);
            return(services);
        }