在C#中进行Owin令牌授权后调用函数处理程序
本文关键字:调用 函数 处理 程序 授权 令牌 Owin | 更新日期: 2023-09-27 17:57:35
我在服务器端使用Owin来获取访问令牌。然后,我将令牌从客户端发送到要进行身份验证的服务器。我想知道,如何在授权完成后运行函数?(我尝试过AccessTokenProvider->OnReceiveAsync,但没有正常工作)。
我找到了答案。在应用程序启动或启动Oauth对象时,使用OnReceive而不是OnReceiveAsync就足够了,如下所示:
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/GetAuthToken",
Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin",
AccessTokenExpireTimeSpan = TimeSpan.FromDays(10),
AllowInsecureHttp = true,
AccessTokenProvider = new AuthenticationTokenProvider()
{
OnReceive = context =>
{
context.DeserializeTicket(context.Token);
// After this you can be sure that your ticket is initialized and have
// an access to the user
if(context.Ticket.Identity.IsAuthenticated)
EntityContext.UserId = context.Ticket.Identity.GetUserId();
else
EntityContext.UserId = "";
}
}
};