Exemplo n.º 1
0
 public virtual Task Authenticated(OAuth2AuthenticatedContext context)
 {
     return(OnAuthenticated(context));
 }
 public virtual Task Authenticated(OAuth2AuthenticatedContext context)
 {
     return OnAuthenticated(context);
 }
Exemplo n.º 3
0
        protected override async Task <AuthenticationTicket> AuthenticateCore()
        {
            Logger.WriteVerbose("AuthenticateCore");
            AuthenticationExtra extra = null;

            try
            {
                var      query = this.Request.GetQuery();
                string[] lookup;
                string   code  = null;
                string   state = null;

                if (query.TryGetValue("code", out lookup) &&
                    lookup != null &&
                    lookup.Length == 1)
                {
                    code = lookup[0];
                }

                if (code == null)
                {
                    return(null);
                }

                if (query.TryGetValue("state", out lookup) &&
                    lookup != null &&
                    lookup.Length == 1)
                {
                    state = lookup[0];
                }

                extra = Options.StateDataHandler.Unprotect(state);

                if (extra == null)
                {
                    return(null);
                }

                if (ValidateCorrelationId(extra, Logger))
                {
                    var accessToken = await GetAccessToken(code);

                    if (accessToken != null)
                    {
                        var userInfo = await GetUserInfo(accessToken);

                        var authenticatedContext = new OAuth2AuthenticatedContext(
                            accessToken,
                            userInfo.UserId,
                            userInfo.UserName,
                            Request.Environment)
                        {
                            Identity = new ClaimsIdentity(
                                Options.AuthenticationType,
                                "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
                                "http://schemas.microsoft.com/ws/2008/06/identity/claims/role"),
                            Extra = extra
                        };

                        if (!string.IsNullOrWhiteSpace(authenticatedContext.UserId))
                        {
                            authenticatedContext.Identity.AddClaim(
                                new Claim(
                                    "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier",
                                    authenticatedContext.UserId,
                                    "http://www.w3.org/2001/XMLSchema#string",
                                    Options.AuthenticationType));
                        }

                        if (!string.IsNullOrWhiteSpace(authenticatedContext.UserName))
                        {
                            authenticatedContext.Identity.AddClaim(
                                new Claim(
                                    "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name",
                                    authenticatedContext.UserName,
                                    "http://www.w3.org/2001/XMLSchema#string",
                                    Options.AuthenticationType));
                        }

                        await Options.Provider.Authenticated(authenticatedContext);

                        return(new AuthenticationTicket(
                                   authenticatedContext.Identity,
                                   authenticatedContext.Extra));
                    }
                }
            }
            catch (Exception e)
            {
                Logger.WriteError(e.Message);
            }

            return(new AuthenticationTicket(null, extra));
        }