C# 从OWIN OAuth身份验证服务器检索用户标识
我的项目中有两个MVC web应用程序。第一个充当OAuth身份验证服务器,只处理登录和用户身份验证,而第二个是带有一些公共和受保护控制器的标准MVC应用程序 我在Auth服务器中使用自定义OWINC# 从OWIN OAuth身份验证服务器检索用户标识,c#,asp.net-mvc,authentication,oauth,owin,C#,Asp.net Mvc,Authentication,Oauth,Owin,我的项目中有两个MVC web应用程序。第一个充当OAuth身份验证服务器,只处理登录和用户身份验证,而第二个是带有一些公共和受保护控制器的标准MVC应用程序 我在Auth服务器中使用自定义OWINOAuthAuthorizationServerProvider,在客户端应用程序中使用DotNetOpenAuth 目前,我可以将我的客户端应用程序重定向到auth服务器的登录页面,使用查询字符串中的访问令牌登录并重定向回去,目前我对此很满意。但是,我需要在客户端应用程序中访问User.Identi
OAuthAuthorizationServerProvider
,在客户端应用程序中使用DotNetOpenAuth
目前,我可以将我的客户端应用程序重定向到auth服务器的登录页面,使用查询字符串中的访问令牌登录并重定向回去,目前我对此很满意。但是,我需要在客户端应用程序中访问User.Identity
,以获取用户Id、用户声明、是否经过身份验证等
我注意到OwinAuthenticationTokenProvider
可以接收访问令牌并将其反序列化为AuthenticationTokenReceiveContext
,其中包含身份对象,但是我无法从我的客户端应用程序中找到有关如何实现这一点的任何信息
如何将访问令牌从OAuth服务器本身外部的应用程序交换为用户身份
我一直在使用这个演练:但是它使用WebAPI作为资源服务器,并没有详细介绍如何获取用户
My身份验证服务器启动.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(像这样保存令牌是临时的)
公共类AuthenticationCodeProvider:AuthenticationTokenProvider
{
专用只读ConcurrentDictionary\u身份验证代码=
新的ConcurrentDictionary(StringComparer.Ordinal);
公共身份验证代码提供程序()
{
OnCreate+=CreateAuthenticationCode;
OnReceive+=ReceiveAuthenticationCode;
}
私有void CreateAuthenticationCode(AuthenticationTokenCreateContext上下文)
{
context.SetToken(Guid.NewGuid().ToString(“n”)+Guid.NewGuid().ToString(“n”));
_authenticationCodes[context.Token]=context.SerializeTicket();
}
私有void ReceiveAuthenticationCode(AuthenticationTokenReceiveContext上下文)
{
字符串值;
if(_authenticationCodes.TryRemove(context.Token,out值))
{
DeserializeTicket(值);
}
}
}
OAuth不进行身份验证或身份验证。您应该查看OpenID Connect。我找到了这个链接:这可能有助于您制定策略。@jwilleke OAuth只是协议,正如我所说,我已经在验证用户身份(使用Google或OpenID Connect)。我的问题是,如何从我的客户端应用程序中获取用户的详细信息?如果您使用ODIC并使用授权码请求,您应该同时收到访问令牌和身份令牌。有关用户信息,请将标识令牌提供给ODIC提供程序的用户\信息端点。@jwilleke什么是ODIC?OpenID Connect。(ODIC)。
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);
}
}
}