コード例 #1
0
        //public async Task<IManagementSession> GetSessionAsync(ClaimsIdentity identity)
        //{
        //  var claim = identity.FindFirst(ManagementSession.AccessTokenClaimType);
        //  if (claim == null) return null;

        //  string token = claim.Value;

        //  IManagementSession session;

        //  if (!sessions.TryGetValue(token, out session))
        //  {
        //    try
        //    {
        //      session = new ManagementSession(this, identity);
        //      if (await session.Validate())
        //      {
        //        if (sessions != null) sessions[token] = session;
        //      }
        //      else
        //      {
        //        session.Dispose();
        //        session = null;
        //      }
        //    }
        //    catch { }
        //  }

        //  return session;
        //}

        /// <summary>
        /// Loggt den Nutzer an der Web-Application an
        /// </summary>
        /// <param name="username">Anmeldenamen</param>
        /// <param name="password">password des Nutzers</param>
        /// <returns></returns>
        public async Task <IManagementSession> LoginAsync(string username, string password)
        {
            const string CONTENT       = "username={0}&password={1}&grant_type=password";
            var          stringContent = String.Format(CONTENT, username, password);

            stringContent = this.ClientInformation.AddClientData(stringContent);
            var content = new StringContent(stringContent);

            //var content = new ObjectContent(typeof(object), new { username, Password = password, grant_type = "password" }, new JsonMediaTypeFormatter());
            HttpResponseMessage response = await client.PostAsync(LOGIN_PATH, content);

            if (response.IsSuccessStatusCode)
            {
                var accessResult = await response.Content.ReadAsAsync <AuthenticationResultModel>();

                var session = new ManagementSession(this, username, accessResult);

                sessions[session.Token] = session;

                return(session);
            }
            throw new ServerException(response);
        }
コード例 #2
0
        //public async Task<IManagementSession> GetSessionAsync(ClaimsIdentity identity)
        //{
        //  var claim = identity.FindFirst(ManagementSession.AccessTokenClaimType);
        //  if (claim == null) return null;

        //  string token = claim.Value;

        //  IManagementSession session;

        //  if (!sessions.TryGetValue(token, out session))
        //  {
        //    try
        //    {
        //      session = new ManagementSession(this, identity);
        //      if (await session.Validate())
        //      {
        //        if (sessions != null) sessions[token] = session;
        //      }
        //      else
        //      {
        //        session.Dispose();
        //        session = null;
        //      }
        //    }
        //    catch { }
        //  }

        //  return session;
        //}

        /// <summary>
        /// Loggt den Nutzer an der Web-Application an
        /// </summary>
        /// <param name="username">Anmeldenamen</param>
        /// <param name="password">password des Nutzers</param>
        /// <returns></returns>
        public async Task<IManagementSession> LoginAsync(string username, string password)
        {
            const string CONTENT = "username={0}&password={1}&grant_type=password";
            var stringContent = String.Format(CONTENT, username, password);
            stringContent = this.ClientInformation.AddClientData(stringContent);
            var content = new StringContent(stringContent);

            //var content = new ObjectContent(typeof(object), new { username, Password = password, grant_type = "password" }, new JsonMediaTypeFormatter());
            HttpResponseMessage response = await client.PostAsync(LOGIN_PATH, content);

            if (response.IsSuccessStatusCode)
            {
                var accessResult = await response.Content.ReadAsAsync<AuthenticationResultModel>();
                var session = new ManagementSession(this, username, accessResult);

                sessions[session.Token] = session;

                return session;
            }
            throw new ServerException(response);
        }