private void AddClaim(MyMailAuthenticatedContext context, string type, string value) { if (!string.IsNullOrEmpty(value)) { context.Identity.AddClaim(new Claim(type, value, XmlSchemaString, Options.AuthenticationType)); } }
public virtual Task Authenticated(MyMailAuthenticatedContext context) { return(OnAuthenticated(context)); }
protected override async Task <AuthenticationTicket> AuthenticateCoreAsync() { AuthenticationProperties properties = null; try { string code = string.Empty; IReadableStringCollection query = Request.Query; IList <string> values = query.GetValues("code"); if (values != null && values.Count == 1) { code = values[0]; } properties = Options.StateDataFormat.Unprotect(Options.StoreState); if (properties == null) { return(null); } // OAuth2 10.12 CSRF if (!ValidateCorrelationId(properties, _logger)) { return(new AuthenticationTicket(null, properties)); } string requestPrefix = Request.Scheme + Uri.SchemeDelimiter + Request.Host; string redirectUri = requestPrefix + Request.PathBase + Options.CallbackPath; IDictionary <string, string> parameters = new Dictionary <string, string>(); parameters.Add("client_id", Options.ClientId); parameters.Add("client_secret", Options.ClientSecret); parameters.Add("grant_type", "authorization_code"); parameters.Add("code", code); parameters.Add("redirect_uri", redirectUri); using (HttpContent httpContent = new FormUrlEncodedContent(parameters.ToList())) { using (HttpResponseMessage responseMessage = await _httpClient.PostAsync("https://connect.mail.ru/oauth/token", httpContent)) { if (responseMessage.IsSuccessStatusCode) { string s = await responseMessage.Content.ReadAsStringAsync(); var dynamicObject = JsonConvert.DeserializeObject <dynamic>(s); string accessToken = dynamicObject["access_token"]; string expires = dynamicObject["expires_in"]; string userid = dynamicObject["x_mailru_vid"]; var myMailClient = new MyMailClient(Options.ClientId, userid, accessToken, Options.PrivateKey); string userInfoRequestUri = myMailClient.BuildMethodRequestUri("users.getInfo"); s = await _httpClient.GetStringAsync(userInfoRequestUri); JArray userInfoArray = JArray.Parse(s); var context = new MyMailAuthenticatedContext(Context, (JObject)userInfoArray.First(), accessToken, expires) { Identity = new ClaimsIdentity( Options.AuthenticationType, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType) }; AddClaim(context, "urn:mymail:accesstoken", context.AccessToken); AddClaim(context, "urn:mymail:link", context.Link); AddClaim(context, ClaimTypes.NameIdentifier, context.Id); AddClaim(context, ClaimTypes.Name, context.FullName); AddClaim(context, ClaimTypes.GivenName, context.FirstName); AddClaim(context, ClaimTypes.Surname, context.LastName); AddClaim(context, ClaimTypes.Email, context.Email); context.Properties = properties; await Options.Provider.Authenticated(context); return(new AuthenticationTicket(context.Identity, context.Properties)); } } } } catch (Exception ex) { _logger.WriteError(ex.Message); } return(new AuthenticationTicket(null, properties)); }
public virtual Task Authenticated(MyMailAuthenticatedContext context) { return OnAuthenticated(context); }