asp.net身份-身份验证代码授予-释放模式下的意外行为

时间:2019-04-26 15:32:17

标签: c# asp.net oauth asp.net-web-api2 asp.net-identity

我试图将Authorization Code Grant添加到使用ASP.net Identity 2.2.2的现有解决方案中。

基于Example-Code,我在**Startup.Auth.cs**中配置了Cookie身份验证,如下所示:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
 AuthenticationType =  "Application",
 AuthenticationMode = AuthenticationMode.Passive,
 LoginPath = new PathString("/Oauth/Login"),
 LogoutPath = new PathString("/Oauth/Logout"),
 CookieName = CookieAuthenticationDefaults.CookiePrefix + "SuperSecretName",
});

OAuthController.cs中,我使用了MS演示解决方案中的代码:

public async Task<ActionResult> Authorize()
 {
            try
            {
                if (Response.StatusCode != 200)
                {
                    return RedirectToAction("AuthorizeError");
                }

            var authentication = HttpContext.GetOwinContext().Authentication;


                if (authentication == null)
                {
                    throw new Exception("Authentication Middleware not available.");
                }

                var ticket = await authentication.AuthenticateAsync("Application");
                var identity = ticket != null ? ticket.Identity : null;

                if (identity == null)
                {
                    authentication.Challenge("Application");
                    return new HttpUnauthorizedResult();
                }

                var scopes = (Request.QueryString.Get("scope") ?? "").Split(' ');

                if (Request.HttpMethod == "POST")
                {
                    if (!string.IsNullOrEmpty(Request.Form.Get("submit.Grant")))
                    {
                        identity = new ClaimsIdentity(identity.Claims, "Bearer", identity.NameClaimType, identity.RoleClaimType);

                        foreach (var scope in scopes)
                        {
                            identity.AddClaim(new Claim("urn:oauth:scope", scope));
                        }

                        authentication.SignIn(identity);
                    }

                    if (!string.IsNullOrEmpty(Request.Form.Get("submit.Login")))
                    {
                        authentication.SignOut("Application");
                        authentication.Challenge("Application");
                        return new HttpUnauthorizedResult();
                    }
                }

                return View();
            }
            catch(Exception ex)
            {
                throw;
            }
        }

使用“ DEBUG”编译时,应用程序的行为类似于预期的: 成功登录后,应用程序通过HTTP 302将用户重定向回去,并且客户端收到访问令牌和刷新令牌。

在“释放”模式下,HTTP 302的重定向失败(无例外)。我看到的是401,而不是302,进程卡住了。

CookieAuthenticationOptions中的documentation告诉我们,设置了LoginPath参数后,中间件会自动从401切换到302。

  

LoginPath属性通知中间件它应该将传出的401未经授权状态代码更改为302重定向到给定的登录路径。生成401的当前URL作为由ReturnUrlParameter命名的查询字符串参数添加到LoginPath中。   一旦对LoginPath的请求授予了新的SignIn身份,则ReturnUrlParameter值将用于将浏览器重定向回导致原始未经授权状态代码的url。如果LoginPath为null或为空,则中间件将不会查找401未经授权的状态代码,并且在发生登录时不会自动重定向。

从“调试”模式切换到“发布”模式时,使用MS Demo App可获得相同的结果。

有什么建议吗?

预先感谢, 基督徒

0 个答案:

没有答案