3

一旦谷歌登录过程完成,我就会得到localhost(portnumber)://signin-google?state=blahblahblah...。

下面是 API 运行的主要入口点。

static void Main(string[] args)
        {
            using (var app = WebApp.Start<Startup>("http://localhost:8090"))
            {
                Process.Start("http://localhost:8090/api");
                Console.WriteLine("Close this window to stop this server");
                Console.ReadLine();

            }
        }

下面是我的 startup.cs 类代码

public void Configuration(IAppBuilder app)
        {
            var cookieOpts = new CookieAuthenticationOptions
            {
                LoginPath = new PathString("/account/login"),
                CookieSecure = CookieSecureOption.SameAsRequest
            };
            app.UseCookieAuthentication(cookieOpts);

            app.SetDefaultSignInAsAuthenticationType(cookieOpts.AuthenticationType);
            var googleOpts = new GoogleOAuth2AuthenticationOptions
            {
                ClientId = "XXXXXXXXX",
                ClientSecret = "XXXXXXXXX"
            };
            app.UseGoogleAuthentication(googleOpts);

            var config = new HttpConfiguration();
            config.Services.Replace(typeof(IHttpControllerTypeResolver), new ControllerResolver());
            config.MapHttpAttributeRoutes();

            app.UseWebApi(config);
        }

执行谷歌登录挑战页面的控制器是

 [RoutePrefix("account"), AllowAnonymous]
    public class AccountController : ApiController
    {
        [HttpGet, Route("login")]
        public IHttpActionResult Login(string returnUrl)
        {
            var authProps = new AuthenticationProperties
            {
                RedirectUri = returnUrl
            };
            Request.GetOwinContext().Authentication.Challenge(authProps, "Google");
            return StatusCode(HttpStatusCode.Unauthorized);
        }

        [HttpGet, Route("logoff")]
        public IHttpActionResult Logout()
        {
            Request.GetOwinContext().Authentication.SignOut();
            return Ok();
        }
    }

我使用谷歌授权的控制器是

 [RoutePrefix("api"), Authorize]
    public class DefaultController : ApiController
    {
        [HttpGet, Route("")]
        public IHttpActionResult Get()
        {
            return Ok("Hello, world!");
        }
    }

一切正常 1. 当应用程序启动时,它会检查谷歌登录 2. 如果没有,它将重定向到谷歌登录页面 3. 登录谷歌后,它会重定向回 l​​ocalhost(portnumber)://signin-google?state =哈哈哈哈哈哈。。

我想它应该重定向到实际的 api - 在我的情况下它应该有http://localhost:portnumer:/api

以下是用于快速参考的跟踪捕获文件 - Trace-log-is-here

先感谢您..:)

源代码 - https://github.com/BDChaudhari90/OAuthGoogleSelfHost

4

0 回答 0