c# - 序列包含多个元素 Microsoft.Owin.Security.AuthenticationManager

标签 c# oauth-2.0 asp.net-web-api2 owin google-authentication

尝试使用 Google 进行外部身份验证时,应用程序出现以下异常:

<Error> <Message>An error has occurred. <ExceptionMessage>Sequence contains more than one element</ExceptionMessage> <ExceptionType>System.InvalidOperationException</ExceptionType> <StackTrace> at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable1 source) at Microsoft.Owin.Security.AuthenticationManager.<AuthenticateAsync>d__8.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at System.Web.Http.HostAuthenticationFilter.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at System.Web.Http.Controllers.AuthenticationFilterResult.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()

我已按如下方式配置我的 Web Api oAuth:

public void ConfigureOAuth(IAppBuilder app)
{
    app.UseExternalSignInCookie(
        Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);

    OAuthBearerOptions = new OAuthBearerAuthenticationOptions();

    OAuthAuthorizationServerOptions OAuthServerOptions = 
        new OAuthAuthorizationServerOptions()
    {
        AllowInsecureHttp = true,
        TokenEndpointPath = new PathString("/token"),
        AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
        Provider = new SimpleAuthorizationServerProvider(),
    };

    app.UseOAuthAuthorizationServer(OAuthServerOptions);

    app.UseOAuthBearerAuthentication(OAuthBearerOptions);

    googleAuthOptions = new GoogleOAuth2AuthenticationOptions()
    {
        ClientId = ClientId,
        ClientSecret = ClientSecret,
        Provider = new GoogleAuthProvider()
    };

    app.UseGoogleAuthentication(googleAuthOptions);
}

最佳答案

请检查,可能是您使用的 app.UseOAuthBearerTokens(OAuthOptions);app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());一起

关于c# - 序列包含多个元素 Microsoft.Owin.Security.AuthenticationManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27101402/

相关文章:

c# - 设置默认签名外观

c# - 提取sql连接字符串的属性

c# - 在运行时动态添加 C# 属性

oauth-2.0 - Microsoft Graph API Authentication_MissingOrMalformed

c# - Camel Case Web API Json 设置似乎不起作用

c# - Autofac:使用 Web API 2 设置,我缺少什么

c# - 为什么这有时意味着基础?

authentication - 授权服务器上的 session 起什么作用?

ruby-on-rails - Omniauth、Devise、Open ID、CanCan - 什么以及何时使用 Rails API 应用程序的解决方案

c# - 与 Web API 共享的 MVC 身份验证