我正在为我的应用程序使用owin,使用Web Api2处理数据和使用MVC5进行查看.
static Startup()
{
PublicClientId = "self";
UserManagerFactory = () => new UserManager<User, int>(new UserRepository());
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/Account/Token"),
AuthorizeEndpointPath = new PathString("/Account/ExternalLogin"),
Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
RefreshTokenProvider = new SimpleRefreshTokenProvider(),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(59),
AllowInsecureHttp = true,
};
}
Run Code Online (Sandbox Code Playgroud)
当用户通过/ Token登录?username = x&password = x&grant_type = password时,它会生成带有刷新GUID的承载令牌,以便将来刷新令牌/令牌?grant_type = refresh_token,所有工作都正常.
我的应用程序可以通过Facebook等登录,它从Facebook获取我需要的数据(电子邮件等)然后我想用刷新GUID生成令牌,问题是刷新我需要通过/ Token传递数据吗?用户名和密码但我的用户没有没有密码,我很难发送额外的参数Scope并显示其使用的提供商和提供商密钥,并在DB中搜索此用户已注册并授予他生成令牌的权限.
这是个好主意吗?

我找到了解决方案,无需使用 garnt_type=password:
这是我的 AuthroizationServer 正在使用的刷新令牌类
public class SimpleRefreshTokenProvider : IAuthenticationTokenProvider
{
private static ConcurrentDictionary<string, AuthenticationTicket> _refreshTokens =
new ConcurrentDictionary<string, AuthenticationTicket>();
public async Task CreateAsync(AuthenticationTokenCreateContext context)
{.......}
public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
{.......}
}
Run Code Online (Sandbox Code Playgroud)
我的 AccountController 初始化如下:
public AccountController()
: this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat)
{
}
public AccountController(UserManager<User, int> userManager, ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
{
UserManager = userManager;
AccessTokenFormat = accessTokenFormat;
UserManager.UserValidator = new CustomUserValidator<User>(UserManager);
}
Run Code Online (Sandbox Code Playgroud)
现在我需要创建 AuthenticationTokenCreateContext 并将其发送到我的 RefreshTokenProvider,以便它将在 _refreshTokens 中添加刷新 Guid,这样当我发送我的refreshGuid 时它将是有效的。我在哪里为外部用户创建身份
ClaimsIdentity oAuthIdentity = await UserManager.CreateIdentityAsync(user,
OAuthDefaults.AuthenticationType);
AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.Id.ToString());
oAuthIdentity.AddClaim(new Claim("TokenGuid", Guid.NewGuid().ToString()));
var ticket = new AuthenticationTicket(oAuthIdentity, properties);
await Startup.OAuthOptions.RefreshTokenProvider.CreateAsync(new AuthenticationTokenCreateContext(Request.GetOwinContext(), AccessTokenFormat, ticket));
Authentication.SignIn(properties, oAuthIdentity);
Run Code Online (Sandbox Code Playgroud)
现在我所有的令牌都可以刷新,因为我将 TokenGuid 传递给 RefreshTokenProvider,之后我使用该 GUID 创建令牌,下次我将刷新它 /Token?grant_type=refresh_token.....
PS 如果您需要详细帮助,可以写信给我。