Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP NET核心3-忘记密码500错误_C#_Asp.net Core - Fatal编程技术网

C# ASP NET核心3-忘记密码500错误

C# ASP NET核心3-忘记密码500错误,c#,asp.net-core,C#,Asp.net Core,我有一个ASP NET Core 3项目,我添加了标识。我还没有对忘记密码页面做任何操作,并且第一次对它进行了测试。我收到一个500错误,这个错误让我困惑。你能帮我调试一下吗 fail: Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer[2] Connection ID "18158513729233223683", Request ID "80000006-0007-fc00-b63f-84710c7967bb": An unha

我有一个ASP NET Core 3项目,我添加了标识。我还没有对忘记密码页面做任何操作,并且第一次对它进行了测试。我收到一个500错误,这个错误让我困惑。你能帮我调试一下吗

fail: Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer[2]
      Connection ID "18158513729233223683", Request ID "80000006-0007-fc00-b63f-84710c7967bb": An unhandled exception was thrown by the application.
System.InvalidOperationException: Unable to resolve service for type 'System.String' while attempting to activate 'LFPortal.Services.EmailSender'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.<>c__DisplayClass7_0.<GetCallSite>b__0(Type type)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Type serviceType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
   at lambda_method(Closure , IServiceProvider , Object[] )
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DefaultPageModelActivatorProvider.<>c__DisplayClass1_0.<CreateActivator>b__0(PageContext context)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.DefaultPageModelFactoryProvider.<>c__DisplayClass3_0.<CreateModelFactory>b__0(PageContext pageContext)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.CreateInstance()
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageActionInvoker.InvokeInnerFilterAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 92.0026ms 500 
这是ConfigureServices部分

            services.AddTransient<IEmailSender, EmailSender>(i =>
                new EmailSender(
                    Configuration["EmailSender:Host"],
                    Configuration.GetValue<int>("EmailSender:Port"),
                    Configuration.GetValue<bool>("EmailSender:EnableSSL"),
                    Configuration["EmailSender:UserName"],
                    Configuration["EmailSender:Password"]
                )
            );
        services.AddTransient<IEmailSender, EmailSender>(i =>
            new EmailSender(
                Configuration
            )
        );
services.AddTransient(i=>
新邮件发件人(
配置[“EmailSender:主机”],
GetValue(“EmailSender:Port”),
Configuration.GetValue(“EmailSender:EnableSSL”),
配置[“EmailSender:用户名”],
配置[“EmailSender:密码”]
)
);
我来试试:

对于EmailSender类

public class EmailSender : IEmailSender
    {
        // Our private configuration variables
        private string host;
        private int port;
        private bool enableSSL;
        private string userName;
        private string password;

        // Get our parameterized configuration
        public EmailSender(string host, int port, bool enableSSL, string userName, string password)
        {
            this.host = host;
            this.port = port;
            this.enableSSL = enableSSL;
            this.userName = userName;
            this.password = password;
        }

        public Task SendEmailAsync(string email, string subject, string message)
        {
            var client = new SmtpClient(host, port)
            {
                Credentials = new NetworkCredential(userName, password),
                EnableSsl = enableSSL
            };
            return client.SendMailAsync(
                new MailMessage(userName, email, subject, message) { IsBodyHtml = true }
            );
        }
    }
public class EmailSender : IEmailSender
{
    // Our private configuration variables
    private string host;
    private int port;
    private bool enableSSL;
    private string userName;
    private string password;

    // Get our parameterized configuration
    public EmailSender(IConfiguration config)
    {
        this.host = config["EmailSender:Host"],
        this.port = config.GetValue<int>("EmailSender:Port"),
        this.enableSSL = config.GetValue<bool>("EmailSender:EnableSSL"),
        this.userName = config["EmailSender:UserName"],
        this.password = config["EmailSender:Password"]                                   

    }

    public Task SendEmailAsync(string email, string subject, string message)
    {
        var client = new SmtpClient(host, port)
        {
            Credentials = new NetworkCredential(userName, password),
            EnableSsl = enableSSL
        };
        return client.SendMailAsync(
            new MailMessage(userName, email, subject, message) { IsBodyHtml = true }
        );
    }
}

可能EmailSender类在构造函数中需要一个字符串参数?我添加了我的EmailSender类您还可以添加configureServices代码,在其中注册电子邮件发件人吗?添加了configureServices部分可能配置中缺少任何值?您可以尝试从构造函数中删除参数,然后提交配置。然后抛出你自己的异常,如果那里缺少任何值。我不需要断点,因为它修复了它!现在,我需要从其他页面初始化EmailSender对象,那么如何访问Startup.cs之外的配置?我想是这样的!非常感谢。
Public class ExampleController:Controller 
{
     // private field
    private IEmailSender sender;

     //Constructor with Parameter, Dependency Injection should do the rest 
    public ExampleController( IEmailSender eSender)
       {
            // set field
            sender = eSender;
       }

       //your Code,Methods,... where you can use the field.
 }