コード例 #1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var userManager = context.OwinContext.GetUserManager <ApplicationUserManager>();
            var user        = await userManager.FindAsync(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "incorrect_user_pass");
                return;
            }

            if (!user.SmsSent)
            {
                context.SetError("invalid_grant", "please register first.");
                return;
            }

            user.IsLogin = true;
            var isLoginUpdated = await userManager.UpdateAsync(user);

            if (!isLoginUpdated.Succeeded)
            {
                context.SetError("internal_error", "An error has occurred.");
                return;
            }
            try
            {
                var data = await context.Request.ReadFormAsync();

                if (data["deviceId"] != null)
                {
                    var deviceId        = data["deviceId"].ToString();
                    var registerationId = data["registerationId"].ToString();

                    if (string.IsNullOrEmpty(user.NotificationKey))
                    {
                        var notificationKey = Firebase.CreateFirebaseGroup("U-" + user.Id, registerationId);

                        user.NotificationKey = notificationKey;
                        var notificationKeyUpdated = await userManager.UpdateAsync(user);

                        if (!notificationKeyUpdated.Succeeded)
                        {
                            context.SetError("internal_error", "An error has occurred.");
                            return;
                        }
                    }
                    else
                    {
                        Firebase.AddToFirebaseGroup("U-" + user.Id, user.NotificationKey, registerationId);
                    }


                    var oldRegisterId = SetLoginRegisterationId(user.Id, deviceId, registerationId);

                    if (!string.IsNullOrEmpty(oldRegisterId) && oldRegisterId != registerationId)
                    {
                        Firebase.RemoveFromFireBase("U-" + user.Id, user.NotificationKey, new string[] { oldRegisterId });
                    }
                }
                else
                {
                    context.SetError("internal_error", "call setDevice before login.");
                    return;
                }
            }
            catch (Exception ex)
            {
                context.SetError("firebase_error", ex.Message);
            }



            var oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
                                                                     OAuthDefaults.AuthenticationType);

            var cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
                                                                       CookieAuthenticationDefaults.AuthenticationType);

            var properties = CreateProperties(user.Id);
            var ticket     = new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }