我的项目中有两个MVC Web应用程序 . 第一个用作OAuth身份验证服务器,仅处理登录和用户身份验证,而第二个是带有一些公共控制器和一些受保护控制器的标准MVC应用程序 .
我在我的客户端应用程序中使用Auth Server和DotNetOpenAuth中的自定义OWIN OAuthAuthorizationServerProvider
.
目前,我能够将我的客户端应用程序重定向到我的auth服务器's login pages, log in and redirect back with an access token in the query string, which I' m . 但是,我需要访问我的客户端应用程序中的 User.Identity
来执行诸如获取用户ID,他们的声明,是否经过身份验证等操作 .
我注意到Owin AuthenticationTokenProvider
可以接收访问令牌并将其反序列化为 AuthenticationTokenReceiveContext
,其中包含一个标识对象,但是我找不到任何有关如何从我的客户端应用程序发生这种情况的信息 .
如何从OAuth服务器本身外部的应用程序交换用户身份的访问令牌?
我一直在使用这个演练:http://www.asp.net/aspnet/overview/owin-and-katana/owin-oauth-20-authorization-server但它使用WebAPI作为资源服务器,并没有真正详细介绍如何获取用户 .
我的 Authentication Server Startup.Auth.cs
:
public void ConfigureAuth(IAppBuilder app)
{
// authentication provider setup, cookies etc...
var oAuthOptions = new OAuthAuthorizationServerOptions
{
Provider = new ApplicationOAuthProvider(),
AuthorizationCodeProvider = new AuthenticationCodeProvider(),
RefreshTokenProvider = new RefreshCodeProvider(),
AuthorizeEndpointPath = new PathString("/Account/Authorize"),
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(30)
};
app.UseOAuthAuthorizationServer(oAuthOptions);
}
AuthenticationCodeProvider.cs (保存这样的令牌是暂时的)
public class AuthenticationCodeProvider : AuthenticationTokenProvider
{
private readonly ConcurrentDictionary<string, string> _authenticationCodes =
new ConcurrentDictionary<string, string>(StringComparer.Ordinal);
public AuthenticationCodeProvider()
{
OnCreate += CreateAuthenticationCode;
OnReceive += ReceiveAuthenticationCode;
}
private void CreateAuthenticationCode(AuthenticationTokenCreateContext context)
{
context.SetToken(Guid.NewGuid().ToString("n") + Guid.NewGuid().ToString("n"));
_authenticationCodes[context.Token] = context.SerializeTicket();
}
private void ReceiveAuthenticationCode(AuthenticationTokenReceiveContext context)
{
string value;
if (_authenticationCodes.TryRemove(context.Token, out value))
{
context.DeserializeTicket(value);
}
}
}