public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            using (MyUserManager userManager = new MyUserManager())
            {
                IdentityUser user = new IdentityUser();
                if (FindUser(context.UserName, context.Password) != null)
                {
                    user = userManager.FindByName(context.UserName);
                }
                //IdentityUser user2 = await userManager.FindAsync(context.UserName, context.Password);
               
                if (user == null)
                {
                    context.SetError("invalid_grant", "The user name or password is incorrect.");
                    return;
                }

                ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user,
                    context.Options.AuthenticationType);
                ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
                    CookieAuthenticationDefaults.AuthenticationType);
                AuthenticationProperties properties = CreateProperties(user);
                AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
                context.Validated(ticket);
                context.Request.Context.Authentication.SignIn(cookiesIdentity);
            }
        }
Exemplo n.º 2
0
        public override async Task GrantResourceOwnerCredentials(
            OAuthGrantResourceOwnerCredentialsContext context)
        {
            //config to enable cors at localhost domain
            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
            MyUserManager userManager = context.OwinContext.GetUserManager <IdentityConfig.MyUserManager>();
            AppUser       user;

            try
            {
                Debug.WriteLine(context.UserName);
                Debug.WriteLine(context.Password);
                user = await userManager.FindAsync(context.UserName, context.Password);
            }
            catch
            {
                // Could not retrieve the user due to error.
                context.SetError("server_error");
                context.Rejected();
                return;
            }
            if (user != null)
            {
                Debug.WriteLine("Okie");
                ClaimsIdentity identity = await userManager.CreateIdentityAsync(
                    user,
                    DefaultAuthenticationTypes.ExternalBearer);

                context.Validated(identity);
            }
            else
            {
                Debug.WriteLine("Not okie");
                context.SetError("invalid_grant", "Invalid User Id or password'");
                context.Rejected();
            }
        }