예제 #1
0
        public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            var allowedOrigins =
                ProtoOAuthClient.GetAllowedOrigins(context.OwinContext.Get <string>("as:clientAllowedOrigin")).ToArray();

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", allowedOrigins);
            var user       = UserMgr.ProtoUsers.FirstOrDefault(u => u.UserName == context.UserName);
            var passwordOk = user != null && UserMgr.VerifyProtoUserPassword(context.Password, user.PasswordHash);

            if (user == null || !passwordOk || !user.IsActivated)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return(Task.FromResult(-1));
            }

            var identity = GenerateUserIdentity(context, user, UserMgr);
            var props    = new AuthenticationProperties(new Dictionary <string, string> {
                ["as:clientId"]             = context.ClientId ?? string.Empty,
                ["userId"]                  = user.Id,
                ["userName"]                = context.UserName,
                [ClaimTypes.NameIdentifier] = user.Id,
                [ClaimTypes.Name]           = user.UserName
            });

            var ticket = new AuthenticationTicket(identity, props);

            context.Validated(ticket);
            return(Task.FromResult(0));
        }
        public Task ReceiveAsync(AuthenticationTokenReceiveContext context)
        {
            var allowedOrigins =
                ProtoOAuthClient.GetAllowedOrigins(context.OwinContext.Get <string>("as:clientAllowedOrigin")).ToArray();

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", allowedOrigins);

            var hashedTokenId = OAuthMgr.HashSecret(context.Token);

            var refreshToken = OAuthMgr.FindRefreshToken(hashedTokenId);

            if (refreshToken != null)
            {
                //Get protectedTicket from refreshToken class
                context.DeserializeTicket(refreshToken.ProtectedTicket);
                var result = OAuthMgr.RemoveRefreshToken(hashedTokenId);
                return(Task.FromResult(result ? 0 : -1));
            }
            return(Task.FromResult(-2));
        }