public static LiveIdAccessTokenInfo Refresh(string refresh_token) { if (string.IsNullOrEmpty(refresh_token)) { return(null); } var liveIDconf = refresh_token.getGlobalObject <IConfigurationProvider>().getConfiguration <LiveIDConfiguration>(); var secret = refresh_token.getGlobalObject <IConfigurationProvider>().getConfiguration <LiveIDSecret>(); var c = new WebClient(); c.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; var reqparm = new System.Collections.Specialized.NameValueCollection(); reqparm.Add("client_id", liveIDconf.clientId); reqparm.Add("scope", liveIDconf.scopes); reqparm.Add("client_secret", secret.client_secret); reqparm.Add("refresh_token", refresh_token); reqparm.Add("grant_type", "refresh_token"); byte[] responsebytes = c.UploadValues("https://login.microsoftonline.com/common/oauth2/v2.0/token", "POST", reqparm); string json = responsebytes.getString(); LiveIdAccessTokenInfo access_token_info = Newtonsoft.Json.JsonConvert.DeserializeObject <LiveIdAccessTokenInfo>(json); return(access_token_info); }
public static LiveIdAccessTokenInfo Aquire(string code, LiveIDConfiguration liveIDconf) { if (string.IsNullOrEmpty(code)) { return(null); } var secret = code.getGlobalObject <IConfigurationProvider>().getConfiguration <LiveIDSecret>(); var c = new WebClient(); c.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; var reqparm = new System.Collections.Specialized.NameValueCollection(); reqparm.Add("client_id", liveIDconf.clientId); reqparm.Add("redirect_uri", liveIDconf.redirectUri); reqparm.Add("client_secret", secret.client_secret); reqparm.Add("code", code); reqparm.Add("grant_type", "authorization_code"); byte[] responsebytes = c.UploadValues("https://login.microsoftonline.com/common/oauth2/v2.0/token", "POST", reqparm); string json = responsebytes.getString(); LiveIdAccessTokenInfo access_token_info = Newtonsoft.Json.JsonConvert.DeserializeObject <LiveIdAccessTokenInfo>(json); if (!string.IsNullOrWhiteSpace(access_token_info.access_token)) { LiveIdAccessTokenInfoReceived?.Invoke(access_token_info); } return(access_token_info); }
public LiveIdAccessTokenInfo getFreshToken(LiveIdAccessTokenInfo given) { //first let's see we have one in cache var account_info = given.getAccountInfo(); var result = _cache.ContainsKey(account_info?.email) ? _cache[account_info.email] : given; var expiration_date = _expire.ContainsKey(account_info?.email) ? _expire[account_info.email] : DateTime.Now.AddMinutes(-1); if (expiration_date < DateTime.Now) { result = LiveIdAccessTokenInfo.Refresh(result.refresh_token); _cache[account_info.email] = result; _expire[account_info.email] = DateTime.Now.AddMinutes(30); } return(result); }
protected override ActionResult <User> ActionImplemetation() { if (string.IsNullOrEmpty(input)) { return new ActionResult <User> { isSuccessfull = false, message = "code not given" } } ; var liveIDconf = this.getGlobalObject <IConfigurationProvider>().getConfiguration <LiveIDConfiguration>(); LiveIdAccessTokenInfo access_token_info = LiveIdAccessTokenInfo.Aquire(input, liveIDconf); LiveIDAccountInfo account_info = access_token_info.getAccountInfo(); //live id cookies var cd = this.getGlobalObject <IContentDispatcher>(); User user = cd.GetAllByFilter <User>(x => x.email == account_info.email).FirstOrDefault(); //first time in this instance if (user == null) { if (!liveIDconf.allow_new_users) { return(ActionResultTemplates <User> .UnAuthorized); } var cc = this.getGlobalObject <IGlobalContentCreator>(); user = cc.getNewContent <User>(); } user.username = account_info.preferred_username; user.fullname = account_info.name; user.email = account_info.email; cd.Add(user); var session = this.getGlobalObject <MysterySession>(); session.authenticated_user = user; return(new ActionResult <User>(user)); }
public MysteryAuthenticationProvider(LiveIdAccessTokenInfo token) { token = this.getGlobalObject <MicrosowftTokenCache>().getFreshToken(token); _token = token.access_token; }