Я пишу API для android, в котором android отправит мне ключ и провайдера с электронной почтой и именем пользователя, которые мне нужны, чтобы войти или зарегистрировать пользователя с этим внешним провайдером входа в систему, поскольку у меня есть другой класс, в котором я аутентифицирую пользователя наэлектронная почта и пароль, а также создание токена jwt, но теперь я застрял в том, как авторизовать внешнего пользователя и создать для него токен, поскольку у него нет пароля.
Asp.net mvc 5 webapi 2 owin.
КОД
public class AuthorizationServerProvider : OAuthAuthorizationServerProvider
{
private GigJobEntities db = Static.DbContext;
private ApplicationUserManager _userManager;
private ApplicationSignInManager _signInManager;
public HttpContextBase HttpContext { get; }
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
}
public bool IsValidPassword(string password, string hash)
{
PasswordHasher hasher = new PasswordHasher();
PasswordVerificationResult result = hasher.VerifyHashedPassword(hash, password);
if (result == PasswordVerificationResult.Success)
return true;
return false;
}
/// <summary>
/// If user is valid then genrate token and return it :: Khizar Bajwa
/// </summary>
/// <param name="context">UserName & Password & Grant_Type = Password</param>
/// <returns>Verify and genrate token</returns>
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var idenity = new ClaimsIdentity(context.Options.AuthenticationType);
try
{
PasswordHasher passwordHasher = new PasswordHasher();
/*
* * Find user aginst Email :: Khizar Bajwa
*/
var results = db.AspNetUsers.Where(x => x.Email == context.UserName).FirstOrDefault();
/*
* * if results is not null the check create and check password hash :: Khizar Bajwa
*/
if (results != null)
{
var IsValid = IsValidPassword(context.Password, results.PasswordHash.ToString());
/*
* * if user is valided then Genrate Token :: Khizar Bajwa
*/
if (IsValid)
{
idenity.AddClaim(new Claim(ClaimTypes.Role, results.AspNetRoles.ToString()));
idenity.AddClaim(new Claim("username", results.UserName));
idenity.AddClaim(new Claim("user_id", results.Id));
idenity.AddClaim(new Claim(ClaimTypes.Email, results.Email));
context.Validated(idenity);
}
else
{
context.SetError("invalid_grant", "provider usernae and password are incorect");
return;
}
}//Null if Ends
}
catch (Exception ex)
{
context.SetError("invalid_grant", ex.ToString());
return;
}
}//Grant Fun End's Here
}
}