【问题标题】:How to handle Stormpath ID Site JWT response如何处理 Stormpath ID 站点 JWT 响应
【发布时间】:2023-03-31 01:25:01
【问题描述】:

我正在尝试使用 Stormpath ID 站点授权创建 ASP.NET 应用程序。我创建了请求和响应操作并成功获得了帐户。

但是接下来呢?如何告诉应用用户已通过身份验证?

public async Task<RedirectResult> Callback(string jwtResponse)
{
    var client = Request.GetStormpathClient();
    var app = await client.GetApplicationAsync(appUrl);
    var requestDescriptor = HttpRequests.NewRequestDescriptor()
    .WithMethod("GET")
    .WithUri("http://localhost:50084/Auth/Callback?jwtResponse=" + jwtResponse)
    .Build();
    var idSiteListener = app.NewIdSiteAsyncCallbackHandler(requestDescriptor);
    var accountResult = await idSiteListener.GetAccountResultAsync();
    var account = accountResult.GetAccountAsync().Result; //Account
    //What I must do here to tell application that user is authenticated
    return Redirect("/");
}

【问题讨论】:

    标签: c# .net asp.net-mvc asp.net-mvc-4 stormpath


    【解决方案1】:

    您可以将 JWT 交换为 Stormpath 访问令牌,而不是从 ID 站点响应中获取帐户:

    public async Task<RedirectResult> Callback(string jwtResponse)
    {
        var client = Request.GetStormpathClient();
        var app = await client.GetApplicationAsync(appUrl);
    
        var exchangeRequest = new StormpathTokenGrantRequest
        {
            Token = jwtResponse
        });
    
        var grantResponse = await application.ExecuteOauthRequestAsync(exchangeRequest);
    
        // Return grantResponse.AccessTokenString in a secure HTTPOnly cookie, or as a JSON response
    }
    

    如果您使用Stormpath ASP.NET plugin,您可以使用enable ID Site,这将自动为您处理。

    免责声明:我是包作者。

    【讨论】:

      猜你喜欢
      • 2017-05-04
      • 1970-01-01
      • 2018-03-04
      • 2012-05-05
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-16
      相关资源
      最近更新 更多