试图通过一个简单的例子来了解Owin OAuthAuthorizationServer

时间:2014-03-05 00:05:36

标签: oauth owin

受到SPA模板的启发,我希望得到以下工作,但很明显我错过了一些东西。

转到https://localhost:44309/api/Account/ExternalLogin?response_type=token&client_id=self&redirect_uri=%2f时收到错误:返回invalid_request。

在查看SPA模板时,他们使用带路径/ api / Account / ExternalLogin的WebApi控制器?也重定向到提供商。我只是不能让它在下面工作,我的app.Map("/api/Account/ExternalLogin", map =>永远不会触发。

        var OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider("self"),//, UserManagerFactory),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
            AllowInsecureHttp = true,                
        };

        app.UseOAuthAuthorizationServer(OAuthOptions);
        OAuthBearerAuthenticationOptions options2 = new OAuthBearerAuthenticationOptions
        {
            AccessTokenFormat = OAuthOptions.AccessTokenFormat,
            AccessTokenProvider = OAuthOptions.AccessTokenProvider,
            AuthenticationMode = OAuthOptions.AuthenticationMode,
            AuthenticationType = OAuthOptions.AuthenticationType,
            Description = OAuthOptions.Description,
            Provider = new ApplicationOAuthBearerProvider(),
            SystemClock = OAuthOptions.SystemClock
        };
        app.UseOAuthBearerAuthentication(options2);

        app.Map("/api/Account/ExternalLogin", map =>
        {
            map.Run(async ctx =>
            {
                if (!ctx.Authentication.User.Identity.IsAuthenticated)
                {

                    ctx.Authentication.Challenge("...");
                    return;
                }

                var claims = new Claim[] { new Claim("test", "value") };
                ClaimsIdentity identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);
                ctx.Authentication.SignIn(identity);
            });
        });

1 个答案:

答案 0 :(得分:0)

redirect_uri需要是绝对的,而不是像我一样的相对“/”,然后就可以了。