Exemple #1
0
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            AuthenticationProperties properties = null;

            try
            {
                string code  = null;
                string state = null;

                IReadableStringCollection query  = Request.Query;
                IList <string>            values = query.GetValues("code");
                code = values[0];

                values = query.GetValues("state");
                if (values != null && values.Count == 1)
                {
                    state = values[0];
                }

                properties = Options.StateDataFormat.Unprotect(state);

                var oauth2Token = await GetOAuthTokenAsync(code);

                var access = new IFOAuthAccess(oauth2Token);

                if (string.IsNullOrWhiteSpace(access.AccessToken))
                {
                    return(new AuthenticationTicket(null, properties));
                }

                var accountInformation = await GetUserAccountInformation(access.AccessToken);

                var context = new IFOAuthContext(Context, accountInformation, access);
                context.Identity = new ClaimsIdentity(
                    new[]
                {
                    new Claim(ClaimTypes.NameIdentifier, context.Name, ClaimValueTypes.String, Options.AuthenticationType),     // TODO need this id back from user info service
                    new Claim(ClaimTypes.Name, context.Name, ClaimValueTypes.String, Options.AuthenticationType)
                },
                    Options.AuthenticationType,
                    ClaimsIdentity.DefaultNameClaimType,
                    ClaimsIdentity.DefaultRoleClaimType);

                context.Properties = properties;

                access.Persist(Context);

                await Options.Provider.Authenticated(context);

                return(new AuthenticationTicket(context.Identity, context.Properties));
            }
            catch (Exception ex)
            {
                // TODO handle exception
                return(new AuthenticationTicket(null, properties));
            }
        }
Exemple #2
0
 /// <summary>
 /// Called when the user is successfully authenticated
 /// </summary>
 public virtual Task Authenticated(IFOAuthContext context)
 {
     return(OnAuthenticated(context));
 }