在 C# 中的 Owin 令牌授权后调用函数处理程序
Call a Function Handler after Owin Token Authorization in C#
我在服务器端使用 Owin 来获取访问令牌。之后,我将令牌从客户端发送到服务器进行身份验证。我想知道,如何在授权完成后 运行 一个功能? (我试过 AccessTokenProvider -> OnReceiveAsync 但它没有正常工作)。
我找到了答案。在 App Start 或启动 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 = "";
}
}
};
我在服务器端使用 Owin 来获取访问令牌。之后,我将令牌从客户端发送到服务器进行身份验证。我想知道,如何在授权完成后 运行 一个功能? (我试过 AccessTokenProvider -> OnReceiveAsync 但它没有正常工作)。
我找到了答案。在 App Start 或启动 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 = "";
}
}
};