用刷新令牌ASP.NET个人帐户 [英] ASP.NET Individual Accounts with Refresh Token

查看:330
本文介绍了用刷新令牌ASP.NET个人帐户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去保护我的ASP.NET Web API的使用OWIN和ASP.NET的身份,我设法完成它。但我节省了客户端的本地存储(移动),这违背了访问令牌的目的访问令牌。所以,我要补充的刷新令牌。我设法使用访问令牌的同一张票生成刷新令牌。但现在我不知道如何使用刷新标记在客户端。

Im trying to secure my ASP.NET web api using OWIN and ASP.NET identity, I managed to get it done. But I am saving the access token in the client's local storage (Mobile) which defeats the purpose of the access token. So I have to add refresh token. I managed to generate the refresh token using the same ticket of the access token. But now I don't know how to use the refresh token in the client.

Startup.cs

Startup.cs

   OAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider(PublicClientId, UserManagerFactory),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(tokenExpiry),
            AllowInsecureHttp = true,
            RefreshTokenProvider = new AuthenticationTokenProvider
            {
                OnCreate = CreateRefreshToken,
                OnReceive = ReceiveRefreshToken,
            }
        };

     private static void CreateRefreshToken(AuthenticationTokenCreateContext context)
        {
            context.SetToken(context.SerializeTicket());
        }

        private static void ReceiveRefreshToken(AuthenticationTokenReceiveContext context)
        {
            context.DeserializeTicket(context.Token);
        }

AccountController.cs

AccountController.cs

 private JObject GenerateApiToken(IdentityUser user, TimeSpan tokenExpirationTimeSpan, string provider)
        {
            var identity = new ClaimsIdentity(Startup.OAuthOptions.AuthenticationType);

            identity.AddClaim(new Claim(ClaimTypes.Name, user.Id, null, provider));
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, user.Id, null, "LOCAL_AUTHORITY"));



    var ticket = new AuthenticationTicket(identity, new AuthenticationProperties());
        var currentUtc = new Microsoft.Owin.Infrastructure.SystemClock().UtcNow;
        ticket.Properties.IssuedUtc = currentUtc;
        ticket.Properties.ExpiresUtc = currentUtc.Add(tokenExpirationTimeSpan);
        var accesstoken = Startup.OAuthOptions.AccessTokenFormat.Protect(ticket);
        var refreshtoken = Startup.OAuthOptions.RefreshTokenFormat.Protect(ticket);
        Authentication.SignIn(identity);

        // Create the response
        JObject blob = new JObject(
            new JProperty("userName", user.UserName),
            new JProperty("access_token", accesstoken),
            new JProperty("refresh_token", refreshtoken),
            new JProperty("token_type", "bearer"),
            new JProperty("expires_in", tokenExpirationTimeSpan.TotalSeconds.ToString()),
            new JProperty(".issued", ticket.Properties.IssuedUtc.ToString()),
            new JProperty(".expires", ticket.Properties.ExpiresUtc.ToString())
            );
        var json = Newtonsoft.Json.JsonConvert.SerializeObject(blob);
        return blob;
    }

对于承载令牌的客户端请求

Client request for bearer token

 $.ajax({type: 'POST',
                        url: tokenUrl + "Token",
                        data: "grant_type=password&username=" + identity.userName + "&password=" + identity.password,
                        contentType: 'application/x-www-form-urlencoded',
                    }).
                    done(function(response) {

                        app.tokenManager.saveToken(response.access_token, response.refresh_token, response.expires_in, apiTokenType.BASIC);

                        deferred.resolve({
                            token: response.access_token
                        });
                    })
                    .fail(function(result, status) {
                        deferred.reject(result);
                    });

现在,我怎么可以使用刷新标记?

Now, how can I use the Refresh token?

推荐答案

根据aouth2规范
http://tool​​s.ietf.org/html/rfc6749#section-6

according to aouth2 spec http://tools.ietf.org/html/rfc6749#section-6

尝试

POST /token HTTP/1.1
Host: server.example.com
Authorization: Bearer czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded

grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA

这篇关于用刷新令牌ASP.NET个人帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆