Из шаблона SPA мне удалось заставить работать основные потоки OAuth.
OAuthOptions = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
ApplicationCanDisplayErrors = true,
TokenEndpointPath = new Microsoft.Owin.PathString("/Token"),
AuthorizeEndpointPath = new Microsoft.Owin.PathString("/api/Account/ExternalLogin"),
Provider = new CompositeWebroleOauthProvider<User>(PublicClientId, IdentityManagerFactory, CookieOptions)
};
У меня есть одностраничное приложение, размещенное в отдельном домене, которое будет взаимодействовать с webapi, используя токены носителя из конечной точки токена.
Я выполняю поток ResourceOwnerCredentials с запросом со следующими данными:
data: {
grant_type: "password",
username: username,
password: password
}
Эти токены недолговечны и т. д. Теперь я хотел бы расширить свое приложение, чтобы я мог получить токен refress или что-то такое, что мне не нужно все время аутентифицировать. Каковы мои следующие шаги?
Реализация GrantResourceOwnerCredentials:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
using (var identityManager = _identityManagerFactory.Create())
{
var user = await identityManager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
ClaimsIdentity oAuthIdentity = await identityManager.CreateIdentityAsync(user, context.Options.AuthenticationType);
AuthenticationProperties properties = CreatePropertiesAsync(user);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
}
}