स्टार्टअप। प्रमाणीकरण कोड:

            PublicClientId = "self";
            OAuthOptions = new OAuthAuthorizationServerOptions
            {
                TokenEndpointPath = new PathString("/Token"),
                Provider = new FirebirdAuthorizationServerProvider(PublicClientId),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
                AllowInsecureHttp = true
            };

मैं फ़ायरबर्डप्रोवाइडर, संदर्भ का उपयोग कर रहा हूं और प्रोप में मैं एक्सेस टोकन जोड़ता हूं, यह मेरे फ़ंक्शन की परिभाषा है:

  public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
......
                        context.Request.Context.Authentication.SignIn(new AuthenticationProperties { IsPersistent = true }, identity);
                        var ticket = new AuthenticationTicket(identity, props);
                        context.Validated(ticket);
}

टोकन प्राप्त करने के लिए यह मेरा कॉल है:

  public async Task<User> GetUserToken(string username, string password)
        {
            try
            {
                User user = new User();
                if (username == null || password == null || !CrossConnectivity.Current.IsConnected)
                    return null;
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpContent requestContent = new StringContent("grant_type=password&username=" + username + "&password=" + password + "&login_type=" + "sys_dash_users", Encoding.UTF8, "application/x-www-form-urlencoded");
                HttpResponseMessage responseMessage = await client.PostAsync("Token", requestContent);
                if (responseMessage.IsSuccessStatusCode)
                {
                    string jsonMessage;
                    using (Stream responseStream = await responseMessage.Content.ReadAsStreamAsync())
                    {
                        jsonMessage = new StreamReader(responseStream).ReadToEnd();
                    }
                    user = (User)JsonConvert.DeserializeObject(jsonMessage, typeof(User));
                    return user;
                }
                else
                {
                    return null;
                }
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex);
                string error = ex.Message;
                return null;
            }
        }

मुझे नहीं पता कि रीफ्रेश टोकन कैसे बनाया जाता है, और उसके लिए अच्छा ट्यूटोरियल नहीं मिल रहा है। यह कैसे करना है

0
Razzzz 16 मार्च 2020, 11:25

1 उत्तर

सबसे बढ़िया उत्तर

मैंने अपने ही प्रश्न का उत्तर दिया। अगर किसी को इसकी आवश्यकता है तो मेरा कोड है:

जोड़ा गया SimpleRefreshTokenProvider:

    public class SimpleRefreshTokenProvider : IAuthenticationTokenProvider
    {
        private static ConcurrentDictionary<string, AuthenticationTicket> _refreshTokens = new ConcurrentDictionary<string, AuthenticationTicket>();

        public async Task CreateAsync(AuthenticationTokenCreateContext context)
        {
            var guid = Guid.NewGuid().ToString();

            // maybe only create a handle the first time, then re-use for same client
            // copy properties and set the desired lifetime of refresh token
            var refreshTokenProperties = new AuthenticationProperties(context.Ticket.Properties.Dictionary)
            {
                IssuedUtc = context.Ticket.Properties.IssuedUtc,
                ExpiresUtc = DateTime.UtcNow.AddYears(1)
            };
            var refreshTokenTicket = new AuthenticationTicket(context.Ticket.Identity, refreshTokenProperties);

            //_refreshTokens.TryAdd(guid, context.Ticket);
            _refreshTokens.TryAdd(guid, refreshTokenTicket);

            // consider storing only the hash of the handle
            context.SetToken(guid);
        }

        public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
        {
            AuthenticationTicket ticket;
            if (_refreshTokens.TryRemove(context.Token, out ticket))
            {
                context.SetTicket(ticket);
            }
        }

        public void Create(AuthenticationTokenCreateContext context)
        {
            throw new NotImplementedException();
        }

        public void Receive(AuthenticationTokenReceiveContext context)
        {
            throw new NotImplementedException();
        }
    }

और Startup.Authc.cs . में

  OAuthOptions = new OAuthAuthorizationServerOptions
            {
                TokenEndpointPath = new PathString("/Token"),
                Provider = new FirebirdAuthorizationServerProvider(PublicClientId),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
                AllowInsecureHttp = true,
                RefreshTokenProvider = new SimpleRefreshTokenProvider(),


            };
0
Razzzz 16 मार्च 2020, 11:41