我只需要在ApplicationOAuthProvider类中返回IHttpActionResult或json
我尝试了以下代码,但没有奏效。我收到错误“语法错误”。
如果无法返回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);
}
}
}
答案 0 :(得分:1)
不幸的是你做不到。您正在使用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;
}
}