【问题标题】:Extract User details from web api auth token in MVC从 MVC 中的 web api auth token 中提取用户详细信息
【发布时间】:2015-03-11 05:42:27
【问题描述】:

我使用 webapi 项目作为我的身份验证服务器和资源服务器。目的是从 Android 应用程序访问服务。我还想要一个用 MVC 应用程序编写的 Web 前端。我最初使用默认的 MVC 身份验证,但已经转移到 web pai 分发令牌。我可以从 webapi 服务接收身份验证令牌,并且我将令牌发送到 cookie 中的客户端,尽管我可能只是缓存客户端。我目前正在运行以下 OAuthBearerAuthenticationProvider:

public class CookieOAuthBearerProvider : OAuthBearerAuthenticationProvider
{
    public override Task RequestToken(OAuthRequestTokenContext context)
    {
        base.RequestToken(context);
        var value = context.Request.Cookies["AuthToken"];
        if (!string.IsNullOrEmpty(value))
        {
            context.Token = value;
        }
        return Task.FromResult<object>(null);
    }    
}

在我的启动课程中,我有这个方法:

private void ConfigureAuth(IAppBuilder app)
    {

        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
        {

            Provider = new CookieOAuthBearerProvider(),

        });
    }

我在配置方法中调用它。

我似乎缺少的一点是如何利用将我的令牌转换为登录用户。我似乎无法弄清楚反序列化发生在哪里。我尝试将我的 configueAuth 更改为:

private void ConfigureAuth(IAppBuilder app)
    {

        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()
        {

            Provider = new CookieOAuthBearerProvider(),
            AccessTokenProvider = new AuthenticationTokenProvider()
            {

                OnReceive = receive
            }
        });
    }

    public static Action<AuthenticationTokenReceiveContext> receive = new Action<AuthenticationTokenReceiveContext>(c =>
    {
        c.DeserializeTicket(c.Token);
        c.OwinContext.Environment["Properties"] = c.Ticket.Properties;
    });

我的接收方法正在被调用。 AuthenticationTokenReceiveContext 附加了我的令牌,但 DeserializeTicket 返回 null。任何人都可以建议我从这个令牌中获取用户详细信息吗?

按照以下建议的答案进行更新。 Statrup 代码和 OAuthBearerAuthenticationOptions 现在如下所示:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }

    private void ConfigureAuth(IAppBuilder app)
    {

        OAuthOpt = new OAuthBearerAuthenticationOptions()
        {

            Provider = new CookieOAuthBearerProvider(),
            AccessTokenProvider = new AuthenticationTokenProvider()
            {

                OnReceive = receive
            }
        };
        app.UseOAuthBearerAuthentication(OAuthOpt);
    }

    public static Action<AuthenticationTokenReceiveContext> receive = new Action<AuthenticationTokenReceiveContext>(c =>
    {
        var ticket = OAuthOpt.AccessTokenFormat.Unprotect(c.Token);

    });

    public static OAuthBearerAuthenticationOptions OAuthOpt { get; private set; }
}

但我仍然得到一个空值。我是否会遗漏 OAuthBearerAuthenticationOptions 上的一些相关选项?

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-web-api oauth-2.0


    【解决方案1】:

    试试这个。

    将您正在内联实例化的OAuthBearerAuthenticationOptions 保存到Startup.Auth 中名为OAuthOpt(或您喜欢的任何内容)的静态变量中,并在您想要检索用户信息的任何地方使用下面的代码。

    Microsoft.Owin.Security.AuthenticationTicket ticket = Startup.OAuthOpt.AccessTokenFormat.Unprotect(token);` 
    

    我建议您使用Json Web Tokens (JWT) 并使用CustomOAuthProvider 自定义令牌生成。 Here 是来自 Taiseer Joudeh 的关于如何做到这一点的好资源。您必须使用此 nuget package 来解码不记名令牌。

    【讨论】:

    • 我没有在启动验证中使用 OAuthorizationServerOptions。我没有使用烘焙过的东西,因为我想学习如何从头开始添加它。目前我只设置了 UseOAuthBearerAuthentication 因为我不需要在 MVC 应用程序中生成令牌。在反序列化令牌之前是否需要设置 OAuthAuthorizationServerOptions?
    • 嗨,我已经尝试过了,但输出的值仍然为空。我已经用新代码更新了这个问题。也许我错过了 OAuthBearerAuthenticationOptions 设置中的一些相关选项? webApi 和 MVC 应用程序都在同一台测试机器上运行,所以不要认为这是机器密钥反序列化问题。
    • c.Token 在您的receive 方法中的值是多少?
    • 其从web API项目被保护令牌值:T-ZM7z0VklZbg1aIDzM_vD4_QG3JRJn-gTq8dWGzljyUTieYxB_pKQRV5yjiESP0C295ZC52mE795R4q3dDVTyijWienTVobx25SZ4i1jJsrTsyMJLYYbHsZyJ992pZrFGgGs9gsfHTdwfY2ISgfI_veuuKnisuwjIPmbQNuWbpIORAIYT-Z_BlobVPCru_Rtl74PQivBxDOFcZ1tDHO_9RZmam32e3FRe6Iod3FIN0 跨度>
    • 抱歉,我想我这里出了点问题。您是否尝试从 MVC 客户端应用程序中反序列化令牌?
    猜你喜欢
    • 2018-01-18
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 2017-01-28
    相关资源
    最近更新 更多