如何在MVC C#app中将刷新令牌工作流实现到OAUTH工作流中?

时间:2016-07-15 16:43:21

标签: c# oauth

我是OAUTH的新手。我一直在努力将OAUTH实现到我的MVC c#应用程序中以访问ping federate。经过大量的研究,并尝试使用ping federate nuget失败后,我遇到了这个link,最后通过编码示例对整个过程进行了清晰的说明。我遇到了许多我需要访问的端点的通用示例,但从来没有完整的工作流编码示例。在通过一些更改实现该代码并成功将ping用户登录到我的MVC应用程序之后,我开始对刷新令牌进行更多研究。问题...

Q值。我知道如何访问刷新令牌,这意味着我知道在ping联合身份验证用户之后用于刷新访问令牌的端点。但是用于刷新令牌的是什么?它是否用于扩展我的应用程序会话一旦结束?或者它用于如果用户退出我的应用程序,然后他们点击“使用Ping Federate登录”#39;登录链接,只要刷新令牌仍然有效,就不会再次进行身份验证吗?

Q值。如果刷新令牌用于用户第一次进行身份验证后的时间,并且我将刷新令牌保存在数据库中,然后用户使用“使用Ping联合身份验证登录”进行签名。链接在我的登录回来我怎么知道在db中查找刷新令牌的用户是什么用户可以访问我的站点而无需使用ping federate重新验证它们?从那时起,他们就来到了Ping Federate'我不知道他们是谁?

这是我正在使用的以下代码,来自我提供的链接中的用户MatthiasRamp ...我想用以下代码添加我的刷新令牌逻辑。

public async Task<ActionResult> Login(string returnUrl)
{
if (string.IsNullOrEmpty(returnUrl) && Request.UrlReferrer != null)
    returnUrl = Server.UrlEncode(Request.UrlReferrer.PathAndQuery);

if (Url.IsLocalUrl(returnUrl) && !string.IsNullOrEmpty(returnUrl))
    _returnUrl = returnUrl;

//callback function
_redirectUrl = Url.Action("AuthorizationCodeCallback", "ExternalLogin", null, Request.Url.Scheme);

Dictionary<string, string> authorizeArgs = null;
authorizeArgs = new Dictionary<string, string>
{
    {"client_id", "0123456789"}
    ,{"response_type", "code"}
    ,{"scope", "read"}
    ,{"redirect_uri", _redirectUrl}
    // optional: state
};

var content = new FormUrlEncodedContent(authorizeArgs);
var contentAsString = await content.ReadAsStringAsync();
return Redirect("http://localhost:64426/oauth/authorize?" + contentAsString);}

public async Task<ActionResult> AuthorizationCodeCallback()
{
// received authorization code from authorization server
string[] codes = Request.Params.GetValues("code");
var authorizationCode = "";
if (codes.Length > 0)
    authorizationCode = codes[0];

// exchange authorization code at authorization server for an access and refresh token
Dictionary<string, string> post = null;
post = new Dictionary<string, string>
{
    {"client_id", "0123456789"}
    ,{"client_secret", "ClientSecret"}
    ,{"grant_type", "authorization_code"}
    ,{"code", authorizationCode}
    ,{"redirect_uri", _redirectUrl}
};

var client = new HttpClient();
var postContent = new FormUrlEncodedContent(post);
var response = await client.PostAsync("http://localhost:64426/token", postContent);
var content = await response.Content.ReadAsStringAsync();

// received tokens from authorization server
var json = JObject.Parse(content);
_accessToken = json["access_token"].ToString();
_authorizationScheme = json["token_type"].ToString();
_expiresIn = json["expires_in"].ToString();
if (json["refresh_token"] != null)
    _refreshToken = json["refresh_token"].ToString();

//SignIn with Token, SignOut and create new identity for SignIn
Request.Headers.Add("Authorization", _authorizationScheme + " " + _accessToken);
var ctx = Request.GetOwinContext();
var authenticateResult = await ctx.Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalBearer);
ctx.Authentication.SignOut(DefaultAuthenticationTypes.ExternalBearer);
var applicationCookieIdentity = new ClaimsIdentity(authenticateResult.Identity.Claims, DefaultAuthenticationTypes.ApplicationCookie);
ctx.Authentication.SignIn(applicationCookieIdentity);

var ctxUser = ctx.Authentication.User;
var user = Request.RequestContext.HttpContext.User;

//redirect back to the view which required authentication
string decodedUrl = "";
if (!string.IsNullOrEmpty(_returnUrl))
    decodedUrl = Server.UrlDecode(_returnUrl);

if (Url.IsLocalUrl(decodedUrl))
    return Redirect(decodedUrl);
else
    return RedirectToAction("Index", "Home");
}

0 个答案:

没有答案