/// <summary>
        /// Creates an identity result.
        /// </summary>
        /// <param name="user">The user from which to create the identity.</param>
        /// <returns>Returns the newly created identity result.</returns>
        public override Task <IdentityResult> CreateAsync(ApplicationUser user)
        {
            using (IUnitOfWork unitOfWork = new EntityUnitOfWork(new ApplicationDbContext()))
            {
                var activeRealm = unitOfWork.RealmRepository.Get(r => r.ObsoletionTime == null).Single();
                user.RealmId = activeRealm.Id;
            }

            return(base.CreateAsync(user));
        }
Esempio n. 2
0
        /// <summary>
        /// Login to the IMS as the device.
        /// </summary>
        /// <returns>Returns an IPrincipal representing the logged in device or null if the login fails.</returns>
        /// <exception cref="System.InvalidOperationException">If the application is not joined to a realm.</exception>
        public static DeviceIdentity LoginAsDevice()
        {
            DeviceIdentity deviceIdentity = null;

            using (var client = new HttpClient())
                using (var unitOfWork = new EntityUnitOfWork(new ApplicationDbContext()))
                {
                    var realm = unitOfWork.RealmRepository.AsQueryable().SingleOrDefault(r => r.ObsoletionTime == null);

                    if (realm == null)
                    {
                        throw new InvalidOperationException("Not joined to realm");
                    }

                    client.DefaultRequestHeaders.Add("Authorization", "BASIC " + Convert.ToBase64String(Encoding.UTF8.GetBytes(realm.ApplicationId + ":" + realm.ApplicationSecret)));

                    var content = new StringContent($"grant_type=password&username={realm.DeviceId}&password={realm.DeviceSecret}&scope={realm.Address}/imsi");

                    // HACK: have to remove the headers before adding them...
                    content.Headers.Remove("Content-Type");
                    content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

                    var result = client.PostAsync($"{realm.Address}/auth/oauth2_token", content).Result;

                    if (result.IsSuccessStatusCode)
                    {
                        var response = JObject.Parse(result.Content.ReadAsStringAsync().Result);

                        var accessToken = response.GetValue("access_token").ToString();
#if DEBUG
                        Trace.TraceInformation($"Access token: {accessToken}");
#endif
                        var securityToken = new JwtSecurityToken(accessToken);

                        deviceIdentity = new DeviceIdentity(Guid.Parse(securityToken.Claims.First(c => c.Type == "sub").Value), realm.DeviceId, true)
                        {
                            AccessToken = accessToken
                        };
                    }
                }

            return(deviceIdentity);
        }