gpt4 book ai didi

c# - OWIN 外部登录刷新 token

转载 作者:太空狗 更新时间:2023-10-29 23:31:08 25 4
gpt4 key购买 nike

我在我的应用程序中使用 owin,在处理数据时使用 Web Api2,在 View 中使用 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,
};

}

当用户通过/Token?username=x&password=x&grant_type=password登录时,它会生成带有刷新GUID的承载 token ,用于以后的刷新 token /Token?grant_type=refresh_token,一切正常。

我的应用程序可以通过 Facebook 等登录,它从 Facebook(电子邮件等) 获取我需要的数据,然后我想用 刷新 GUID 生成 token ,问题是为了刷新我需要通过/Token?username&password 传递数据但我的用户没有密码,我很难发送额外的参数范围并显示什么提供者和提供者 key 它使用并在数据库中搜索该用户已注册并授予他生成 token 的权限。

这是个好主意吗?

enter image description here

最佳答案

我找到了解决方案,但没有使用 garnt_type=password:

这是 AuthroizationServer 正在使用的我的刷新 token 类

     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)
{.......}
}

我的 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);
}

现在我需要创建 AuthenticationTokenCreateContext 并将其发送到我的 RefreshTokenProvider,以便它会在 _refreshTokens 中添加 Refresh Guid,因此当我发送我的 refreshGuid 时它将有效。我在哪里为 ExternalUser 创建身份

        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);

现在我所有的 token 都可以刷新了,因为我将 TokenGuid 传递给 RefreshTokenProvider,之后我使用该 GUID 创建 token ,下次我将刷新它/Token?grant_type=refresh_token.....

附言如果您需要详细的帮助,可以写信给我。

关于c# - OWIN 外部登录刷新 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24549730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com