自定义UserManager在OAuthAuthorizationServerProvider中不可用

布赖恩

我正在WebApi系统中实现ASP.Net Identity 2。为了管理新帐户的电子邮件确认,我必须创建一个自定义ApplicationUserManager并注册它,以便为每个请求创建该自定义

public class IdentityConfig{
    public static void Initialize(IAppBuilder app)
    {
        [snip]
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
    }

它可以在ApiController内正常工作,如下所示:

public class AccountController : ApiController
{
    public ApplicationUserManager UserManager
    {
        get
        {
            return HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
    }

我面临的问题是ApplicationUserManager.Create在尝试使用OAuth令牌创建方法访问方法之前未调用该方法:

public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        var mgr = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();

在上面的代码中,mgr为null,因为GetUserManager检索null

令牌创建方法是否早已在管道中以某种CreatePerOwinContext方式出现,从而尚未被调用?如果是这样,最好的缓存方式是什么,ApplicationUserManager以便可以在内部使用GrantResourceOwnerCredentials

布赖恩

这个很棘手。事实证明,启动代码必须按一定顺序进行。

此代码将起作用。

public class IdentityConfig{
    public static void Initialize(IAppBuilder app)
    {   
        // correct... first create DB context, then user manager, then register  OAuth Provider
        app.CreatePerOwinContext(AuthContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

        OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new SimpleAuthorizationServerProvider()
        };
        app.UseOAuthAuthorizationServer(OAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
    }
}

如果您更改在应用程序中注册代码的顺序,可能会导致意想不到的后果。此代码导致令牌生成方法GrantResourceOwnerCredentials中的ApplicationUserManager为null

// wrong... these two lines must be after the ApplicationUserManager.Create line
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

[snip]

app.CreatePerOwinContext(AuthContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

当我在这里时,这是另一件事,可能使某人绊倒。这些行导致TokenContext方法GrantResourceOwnerCredentials内部的AuthContext为null

// wrong... The AuthContext must be instantiated before the ApplicationUserManager
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext(AuthContext.Create);

[snip]

app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何添加自定义“网页不可用”

自定义yaml属性在服务类中不可用

Openshift自定义环境变量在Python中不可用

Serverless provider.environment 变量在自定义中不可用

全局对象在自定义模块中不可用,即使传入并请求也是如此

放入流后,我在node-red中的自定义节点不可用

模型不可用于ViewSettingsCustomItem中的自定义控件

当“显示设置”中的分辨率不可用时,如何使用xrandr设置自定义分辨率

解决方案标签在“自定义”下的“设置”标签中不可用,无法上传示例html文件

Spring Security 自定义登录表单重定向到资源不可用

禁用用户登录后,向“此帐户当前不可用”的自定义消息

绑定到活动时,“自定义快速设置”图块仍处于不可用状态

发送自定义IQ时来自服务器的服务不可用响应

如何将网页不可用页面替换为自定义页面?(网络视图)

使用itext将html转换为pdf时,自定义字体在Windows中有效,但在Ubuntu中不可用

自定义Python版本中的zip支持出现问题:zipimport.ZipImportError:无法解压缩数据;zlib不可用

自定义Google助手突然在设备上不可用,但在键入提示时有效

无法在自定义的AOSP上从Google Play安装某些应用:该项目不可用。原因:9

Boost Beast:当内容长度不可用时,如何使用自定义主体产生非块状响应?

使用自定义模块创建NetSuite脚本时,获取“执行定义回调时所有SuiteScript API模块不可用”

使用 Application.Run VBA 调用自定义函数时出现 BERT 错误“宏在此工作簿中可能不可用,或者所有宏都可能被禁用。”

量角器自定义定位器:在生产中不可用,但是在本地主机上可以正常工作

在Identity 2.0中成功实现自定义UserManager <IUser>

Django自定义UserManager中的self.model()

UserManager.FindAsync(用户名,密码)在ASP.NET 5 / Identity 3中不可用

SkipDuplicates 在 nuget 5.0.2 中不可用

Play for Scala 中的连接不可用

CSS资源在JSP中不可用

OSTYPE在Shell脚本中不可用