简体   繁体   English

具有刷新令牌的ASP.NET个人帐户

[英]ASP.NET Individual Accounts with Refresh Token

Im trying to secure my ASP.NET web api using OWIN and ASP.NET identity , I managed to get it done. 我试图使用OWIN和ASP.NET身份保护我的ASP.NET web api,我设法完成它。 But I am saving the access token in the client's local storage (Mobile) which defeats the purpose of the access token. 但是我将访问令牌保存在客户端的本地存储(Mobile)中,这违背了访问令牌的目的。 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? 现在,我该如何使用Refresh令牌?

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

try 尝试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.net Web应用程序个人帐户 - Asp.net Web Application individual accounts Blazor WebAssembly 应用程序与个人帐户和 ASP.NET 核心托管 - 令牌请求 - “错误”:“unauthorized_client” - Blazor WebAssembly App with Individual Accounts and ASP.NET Core Hosted - Token request - "error": "unauthorized_client" 个人用户帐户在ASP.Net MVC中不进行身份验证 - Individual User Accounts to No Authentication in ASP.Net MVC 使用LDAP的Asp.Net Identity个人用户帐户 - Asp.Net Identity Individual User Accounts using LDAP 在ASP.NET中没有个人用户帐户的身份 - Identity without individual user accounts in ASP.NET 在asp.net vnext上使用bearer token身份验证刷新令牌 - refresh token with bearer token authentication on asp.net vnext ASP.NET Web API中的个人帐户错误:用户名或密码不正确 - Individual Accounts in ASP.NET Web API error: username or password is incorrect ASP.NET Core Web API模板中没有单独的用户帐户auth选项 - No Individual User Accounts auth option in ASP.NET Core Web API template 使用ASP.NET身份个人帐户的MVC-如何添加安全性问题? - MVC using ASP.NET Identity Individual Accounts - How to Add Security Questions? Visual Studio 2017中的ASP.NET Web App个人用户帐户身份验证 - ASP.NET Web App Individual User Accounts Authentication in Visual Studio 2017
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM