为令牌中间件提供商启用CORS

时间:2019-06-04 13:48:32

标签: asp.net-web-api

我正在处理ASP API项目,当我仅请求令牌“ / token”时,我遇到了CROS的问题,我知道原因的原因基本上是因为仅针对Web API启用了CORS ,而不是客户端请求的令牌中间件提供商 我添加了这一行代码

context.OwinContext.Response.Headers.Add(“ Access-Control-Allow-Origin”,new [] {“ *”});

在ApplicationOAuthProvider.GrantResourceOwnerCredentials方法中,但不能解决问题

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
    context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
        using (UserManager<IdentityUser> userManager = _userManagerFactory())
        {
            IdentityUser user = await userManager.FindAsync(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }

            ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user,
                context.Options.AuthenticationType);
            ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
                CookieAuthenticationDefaults.AuthenticationType);
            AuthenticationProperties properties = CreateProperties(user.UserName);
            AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);

        }
    }

0 个答案:

没有答案