如何 return Web 中 ApplicationOAuthProvider 中的 IHttpActionResult API

How to return IHttpActionResult in ApplicationOAuthProvider in Web API

我只需要 return IHttpActionResult 或 json 在 ApplicationOAuthProvider class

我尝试了以下代码但没有成功。我收到错误 "Syntax error"。

如果无法 return IHttpActionResult,是否有重定向到 IHttpActionResult 操作方法?

请帮助我几天来我一直在寻找解决方案,但找不到任何帮助。非常感谢您的帮助

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
            {
                var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();

                ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);


                 if (!userManager.CheckPassword(user, context.Password))
                {

                IOwinResponse response = context.Response;
                response.StatusCode = 200;
                response.ContentType = "text/json";
                await response.WriteAsync("{\"Message\":Wrong Password,\"success\":false}");
                return;
                }
}

这是我的完整代码

public class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
            {

         public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
                {
                    var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();

                    ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);


                     if (!userManager.CheckPassword(user, context.Password))
                    {

                    IOwinResponse response = context.Response;
                    response.StatusCode = 200;
                    response.ContentType = "text/json";
                    await response.WriteAsync("{\"Message\":Wrong Password,\"success\":false}");
                    return;
                    }

                    ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
                       OAuthDefaults.AuthenticationType);
                    ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
                        CookieAuthenticationDefaults.AuthenticationType);

                    AuthenticationProperties properties = CreateProperties(user.UserName);
                    AddUserInfoToProperties(properties, user);

                    AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
                    context.Validated(ticket);
                    context.Request.Context.Authentication.SignIn(cookiesIdentity);
                }

                public override Task TokenEndpoint(OAuthTokenEndpointContext context)
                {
                    foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
                    {
                        context.AdditionalResponseParameters.Add(property.Key, property.Value);
                    }

                    return Task.FromResult<object>(null);
                }


                public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
                {
                    // Resource owner password credentials does not provide a client ID.
                    if (context.ClientId == null)
                    {
                        context.Validated();
                    }

                    return Task.FromResult<object>(null);
                }

       public override Task ValidateClientRedirectUri(OAuthValidateClientRedirectUriContext context)
                {
                    if (context.ClientId == _publicClientId)
                    {
                        Uri expectedRootUri = new Uri(context.Request.Uri, "/");

                        if (expectedRootUri.AbsoluteUri == context.RedirectUri)
                        {
                            context.Validated();
                        }
                    }

                    return Task.FromResult<object>(null);
                }
            }
            }

很遗憾,你不能。您正在使用 OAuth2Authorization 中间件,中间件会自行生成响应。

您可以使用SetError()方法,用于返回错误。

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();

    ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);


    if (!userManager.CheckPassword(user, context.Password))
    {
        context.SetError("Wrong Password");
        return;
    }
}