Winforms Can';t使用Web API OWIN JWT令牌验证桌面应用程序
我正在使用windows窗体构建一个桌面应用程序,需要使用令牌身份验证通过WebAPI进行身份验证 API被证明是有效的,因为一个移动应用程序正在使用它,而且我可以使用POSTMAN获得结果 问题是当我从桌面应用程序调用身份验证方法时 当我执行请求时,API将接收该请求,并且它只在身份验证过程中到达Winforms Can';t使用Web API OWIN JWT令牌验证桌面应用程序,winforms,authentication,asp.net-web-api,jwt,owin,Winforms,Authentication,Asp.net Web Api,Jwt,Owin,我正在使用windows窗体构建一个桌面应用程序,需要使用令牌身份验证通过WebAPI进行身份验证 API被证明是有效的,因为一个移动应用程序正在使用它,而且我可以使用POSTMAN获得结果 问题是当我从桌面应用程序调用身份验证方法时 当我执行请求时,API将接收该请求,并且它只在身份验证过程中到达ValidateClientAuthentication(OAuthValidateClientAuthenticationContext),而没有到达GrantResourceOwnerCrede
ValidateClientAuthentication(OAuthValidateClientAuthenticationContext)
,而没有到达GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext)
这是我的CustomAuthProvider
public class CustomOAuthProvider : OAuthAuthorizationServerProvider
{
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
return Task.FromResult<object>(null);
}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var allowedOrigin = "*";
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin });
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "El nombre de usuario o contraseña son incorrectos");
return;
}
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, "JWT");
var ticket = new AuthenticationTicket(oAuthIdentity, null);
context.Validated(ticket);
}
}
目前,我正在尝试两种不同的方法来验证应用程序
第一个:
public LoginResponseModel Authenticate(LoginRequestModel applicationUser)
{
using (var client = new WebClient())
{
try
{
client.Headers["Content-Type"] = "application/json";
var data = applicationUser.Serialize();
var response = client.UploadString(Context.ApiUrl + "Authenticate","POST", JsonConvert.SerializeObject(applicationUser));
var resultJson = JsonConvert.DeserializeObject<LoginResponseModel>(response);
return resultJson;
}
catch (Exception exception)
{
}
}
return null;
}
这应该是回应:
public class LoginResponseModel
{
public string Access_token { get; set; }
public string Token_type { get; set; }
public string Expires_in { get; set; }
}
啊,当两种调用API的方法只到达owin进程的初始验证(ValidateClientAuthentication)时。会发生什么?我怎样才能解决这个问题?我需要做什么才能使流程转到GrantResourceOwnerCredentials
谢谢你的帮助我解决了我的问题。问题是表单没有正确填写和发送
private AuthToken GetAuthToken(LoginRequestModel applicationUser)
{
using (var client = new HttpClient())
{
var form = new Dictionary<string, string>
{
{"grant_type", "password"},
{"username", applicationUser.UserName},
{"password", applicationUser.Password},
};
try
{
var tokenResponse = client.PostAsync(Context.ApiUrl + "Authenticate", new FormUrlEncodedContent(form)).Result;
var token = tokenResponse.Content.ReadAsAsync<AuthToken>(new[] { new JsonMediaTypeFormatter() }).Result;
return token;
}
catch (Exception e)
{
Log4Net.log.Error("Error Getting Auth token", e);
return null;
}
}
}
private AuthToken GetAuthToken(LoginRequestModel应用程序用户)
{
使用(var client=new HttpClient())
{
var form=新字典
{
{“授权类型”,“密码”},
{“username”,applicationUser.username},
{“password”,applicationUser.password},
};
尝试
{
var tokenResponse=client.PostAsync(Context.ApiUrl+“Authenticate”,新FormUrlEncodedContent(form)).Result;
var token=tokenResponse.Content.ReadAsAsync(new[]{new JsonMediaTypeFormatter()});
返回令牌;
}
捕获(例外e)
{
Log4Net.log.Error(“获取身份验证令牌时出错”,e);
返回null;
}
}
}
public class LoginRequestModel
{
public string Grant_type { get; set; } = "Password";
public string UserName { get; set; }
public string Password { get; set; }
}
public class LoginResponseModel
{
public string Access_token { get; set; }
public string Token_type { get; set; }
public string Expires_in { get; set; }
}
private AuthToken GetAuthToken(LoginRequestModel applicationUser)
{
using (var client = new HttpClient())
{
var form = new Dictionary<string, string>
{
{"grant_type", "password"},
{"username", applicationUser.UserName},
{"password", applicationUser.Password},
};
try
{
var tokenResponse = client.PostAsync(Context.ApiUrl + "Authenticate", new FormUrlEncodedContent(form)).Result;
var token = tokenResponse.Content.ReadAsAsync<AuthToken>(new[] { new JsonMediaTypeFormatter() }).Result;
return token;
}
catch (Exception e)
{
Log4Net.log.Error("Error Getting Auth token", e);
return null;
}
}
}