我使用以下guide使用C#实现了一个简单的授权服务器。这适用于我构建的其他应用程序。
当我想登录Auth服务器本身来管理用户设置时出现问题,例如更改密码'或者'更改电子邮件'。我想展示一个前端,但是当我在端点上使用[Authorize]
标记时,我的传入User
始终为null
,而在其他应用程序上工作正常。
我尝试将DefaultAuthenticationType
设置为不同的值但无效。
如何使用它生成的相同令牌登录Auth服务器?
编辑:
为了使流程更加明显:
我有一个网站,api和授权服务器。
当用户想要登录网站时,他们会重定向到授权服务器进行身份验证和授权。这是通过oauth2.0授权代码授权流程。然后,访问令牌用于从受[Authorize
]标记保护的api中检索信息。这很有效。
我现在想要的是我可以重定向到特定的更改密码'查看授权服务器本身,这也必须用[Authorize
]保护。这不起作用,因为user
将为null
。
EDIT2:Authorize标记自动保护c#aspnet mvc / api中的api / view端点。
Edit3:这就是我配置Auth服务器的方式。
public void ConfigureOAuth(IAppBuilder app, IUnityContainer container)
{
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
AuthenticationMode = AuthenticationMode.Passive,
LoginPath = new PathString("/Account/Login"),
LogoutPath = new PathString("/Account/Logout"),
Provider = new CookieAuthenticationProvider()
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationIdentityUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie))
}
});
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
OAuthBearerServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AuthorizeEndpointPath = new PathString("/authorize"),
AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(10),
Provider = container.Resolve<SimpleAuthorizationServerProvider>(),
RefreshTokenProvider = container.Resolve<SimpleRefreshTokenProvider>(),
AuthorizationCodeProvider = container.Resolve<SimpleAuthorizationCodeProvider>()
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthBearerServerOptions);
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
var configuration = container.Resolve<IConfiguration>().Get<AuthServiceSettings>("Application");
// Configure Google
GoogleAuthOptions = new GoogleOAuth2AuthenticationOptions()
{
ClientId = configuration.GoogleSettings.ClientId,
ClientSecret = configuration.GoogleSettings.ClientSecret,
Provider = new GoogleAuthProvider()
};
GoogleAuthOptions.Scope.Add("email");
GoogleAuthOptions.Scope.Add("openid");
app.UseGoogleAuthentication(GoogleAuthOptions);
//Configure Facebook External Login
FacebookAuthOptions = new FacebookAuthenticationOptions()
{
AppId = configuration.FacebookSettings.AppId,
AppSecret = configuration.FacebookSettings.AppSecret,
Provider = new FacebookAuthProvider(),
BackchannelHttpHandler = new FacebookBackChannelHandler(),
UserInformationEndpoint = configuration.FacebookSettings.UserInformationEndpoint
};
FacebookAuthOptions.Scope.Add("email");
FacebookAuthOptions.Scope.Add("public_profile");
app.UseFacebookAuthentication(FacebookAuthOptions);
}
EDIT4:检查变量时的调试信息:
>> HttpContext.User.Identity.IsAuthenticated
false
>> System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated
true
不知道为什么这些身份不一样......但基于此我可以编写自己的授权标签并使用它,但我不愿意使用Thread中的某些东西,因为我不知道如果这适用于多个线程。